It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

change URL on the fly with java?

change URL on the fly with java?

Postby knutso on Thu Mar 13, 2008 7:29 am

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
knutso
 
Posts: 2
Joined: Wed Mar 12, 2008 11:42 pm

Postby knutso on Thu Mar 13, 2008 4:33 pm

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.
knutso
 
Posts: 2
Joined: Wed Mar 12, 2008 11:42 pm

Postby leonard on Mon Mar 17, 2008 4:47 pm

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: Select all
<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: Select all
<body onload="changeLinks();">


cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland


Who is online

Users browsing this forum: No registered users and 2 guests