change URL on the fly with java?
|
| View previous topic :: View next topic |
| Author |
Message |
knutso
Joined: 13 Mar 2008 Posts: 2
|
Posted: Thu Mar 13, 2008 8:29 am Post subject: change URL on the fly with java? |
|
|
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 |
|
 |
|
|
knutso
Joined: 13 Mar 2008 Posts: 2
|
Posted: Thu Mar 13, 2008 5:33 pm Post subject: Re: change URL on the fly with java? |
|
|
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 |
|
 |
leonard 100+ Club

Joined: 18 Dec 2007 Posts: 134 Location: Switzerland
|
Posted: Mon Mar 17, 2008 5:47 pm Post subject: Re: change URL on the fly with java? |
|
|
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 |
|
 |
|
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:
|
|
|
|