Flash Games
 FAQ   Search   Memberlist   Usergroups   Register  Profile   Log in to check your private messages   Log in 


change URL on the fly with java?



 

Post new topic   Reply to topic  
   DEVPPL Forum Index -> JavaScript Forum
View previous topic :: View next topic  
Author Message
knutso



Joined: 13 Mar 2008
Posts: 2

PostPosted: Thu Mar 13, 2008 8:29 am    Post subject: change URL on the fly with java? Reply with quote

I want to make a javascript that can read a a href (URL) link in the main program and change it when it is pressed.

The reason is that I have a program with URLS that I want to change and I cannot change the code, but the program can read a js-script, and the js-script can change the URL's.

Example:

the program gives this url:

http://www.bki.net/tv/Frank/oss.wmv

when the user press the link, it should go like this:

http://www.bki.net/bkitvyy.php?m=Frank/oss.wmv
Back to top
View user's profile Send private message Visit poster's website
knutso



Joined: 13 Mar 2008
Posts: 2

PostPosted: Thu Mar 13, 2008 5:33 pm    Post subject: Re: change URL on the fly with java? Reply with quote

I have found a bit of code tat could lead in the right direction. I am so new to this, and only can suggest.

function readlinks() {
var anchors = document.getElementsByTagName("A");

for( var i=0; i<anchors.length; i++ ) {
if( anchors[i].getAttribute( "href" ) == location.href ) {
anchors[i].setAttribute( "id", "active" );
}
}
}

of course, this must be changed, but possibly a path in the right direction.
Back to top
View user's profile Send private message Visit poster's website
leonard
100+ Club


Joined: 18 Dec 2007
Posts: 134
Location: Switzerland

PostPosted: Mon Mar 17, 2008 5:47 pm    Post subject: Re: change URL on the fly with java? Reply with quote

Hi knutso

Editing the server-side source code would be the far better way to change the URL, but anyway, here is the javascript version. Be aware however, that the URL will only change if javascript is activated.

It always depends on what you exactly want to change, but assuming you dont know the ID of the element and you want to change all href's of all A-TAGs from oldURL to newURL, the script you pasted is almost it. Just change it a little:
Code:

<head>
<script type="text/javascript">
function changeLinks() {
   oldURL = "http://www.bki.net/tv/Frank/oss.wmv";
   newURL = "http://www.bki.net/bkitvyy.php?m=Frank/oss.wmv";
   var anchors = document.getElementsByTagName("A");
   for( var i=0; i<anchors.length; i++ ) {
      if( anchors[i].getAttribute("href") ==  oldURL ) {
         anchors[i].setAttribute("href",newURL ,0);
      }
   }
}
</script>
</head>


Activate the script on load-time:
Code:
<body onload="changeLinks();">


cheers!
- leonard
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    DEVPPL Forum Index -> JavaScript Forum All times are GMT + 1 Hour
Page 1 of 1

 
 
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User: Pass:
Log me on automatically each visit:

 


Powered by phpBB © 2001, 2005 phpBB Group - Modified by DEVPPL

Flash Games - Sitemap