473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

javascript to either redirect or update all url's on a page?

4
Hello,
I am displaying a the html for a web page in a frame on my web page.
We only support IE and are only using Win XP or higher.

That web page contains HREF entries for other url's.
For example:
<A HREF="http://www.myhost.com" >MyHost</A>


Is there a way to use JavaScript to either programmatically dynamically
update all the links or to dynamically modify the link when it is clicked?

My goal is to programmatically end up with the behavior of the below link
without actually modifying the <body> of the original html.

<A HREF="http://localhost:12060/analyze.aspx?url=http://www.myhost.com">MyHost</A>


My problem is that the user can specify any web site to be included in my frame. I would like to add either a script to iterate through the objects in that
html and add a prefix to the HREF= text or to leave the HREF text unchanged
but to catch the OnClick event and then to add the prefix on prior to the
link being processed.

I am new to this and I would appreciate any help.
Thanks,
-Tom
May 18 '07 #1
6 1626
iam_clint
1,208 Expert 1GB
ok so you would do something like this

Expand|Select|Wrap|Line Numbers
  1. //when you want to change the links call this function. it can be onload or whatever... example.   window.onload=updateLinks;
  2. function updateLinks() {
  3.  var links = document.getElementsByTagName("A"); // get an array of all the links.
  4.  for (i=0; i<links.length; i++) { //make a loop to go through them all
  5.    var curlink = links[i].getAttribute("href");  //get the current href attribute.
  6.    links[i].setAttribute("href", "http://localhost:12060/analyze.aspx?url="+curlink); // append your link before the current link.
  7.  }
  8. } // and this is how it could be done.
  9.  
Please reply back.
May 18 '07 #2
MrTom
4
Hi Clint,
Thanks for your help and patience. I am a c++/C# guy and totally new to JavaScript.

I created the below and when it loads, I see "Error on page" on my status line.
Is there a way to see more info about the error?

When I click on the link, It is not redirected.

Thanks again,
-Tom

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script language="JavaScript"><!--
  5. function updateLinks() {
  6.  var links = document.getElementsByTagName("A"); // get an array of all the links.
  7.  for (i=0; i<links.length; i++) { //make a loop to go through them all
  8.    var curlink = links[i].getAttribute("href");  //get the current href attribute.
  9.    links[i].setAttribute("href", "http://localhost:12060/Integrated4.aspx?url="+curlink);
  10. }
  11. //--></script>
  12. </head>
  13.  
  14. <body onload="updateLinks()">
  15.  
  16. <A HREF="http://www.softwarecommitments.com"">softwarecommitments</A>  
  17.  
  18. </body>
  19.  
  20. </html>
  21.  
  22.  
Please reply back.[/quote]
May 21 '07 #3
iam_clint
1,208 Expert 1GB
There is a way to view a more detailed message... on Internet explorer when theres a javascript error there will be a yellow icon in the bottom left hand side of the window and double click it...


but your problem is your missing a }

you have
Expand|Select|Wrap|Line Numbers
  1. function updateLinks() {
  2.  var links = document.getElementsByTagName("A"); // get an array of all the links.
  3.  for (i=0; i<links.length; i++) { //make a loop to go through them all
  4.    var curlink = links[i].getAttribute("href");  //get the current href attribute.
  5.    links[i].setAttribute("href", "http://localhost:12060/Integrated4.aspx?url="+curlink);
  6. }
  7.  
when it should be

Expand|Select|Wrap|Line Numbers
  1. function updateLinks() {
  2.  var links = document.getElementsByTagName("A"); // get an array of all the links.
  3.  for (i=0; i<links.length; i++) { //make a loop to go through them all
  4.    var curlink = links[i].getAttribute("href");  //get the current href attribute.
  5.    links[i].setAttribute("href", "http://localhost:12060/Integrated4.aspx?url="+curlink);
  6. }
  7. }
  8.  
see you open the function { then you open the for loop { then you closed the for loop } but you didn't close the function.
May 21 '07 #4
MrTom
4
Hi Clint,

Thanks for being kind about me missing the closing brace.

That resolved the ''error on page" message.

If I right click and choose properties on the softwarecommitments href,
should I see the updated url or the original?

I am seeing the original.

When I click on it, I go to the original

http://www.softwarecommitments.com
rather than

http://localhost:12060/Integrated4.aspx?url=http://www.softwarecommitments.com

I noticed I had an extra " in the A href which I fixed.

Below is what I am testing.
Thanks again,
-Tom

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <body>
  4.  
  5. <script language="JavaScript"><!--
  6. function updateLinks() {
  7.  var links = document.getElementsByTagName("A");
  8.  for (i=0; i<links.length; i++) {
  9.    var curlink = links[i].getAttribute("href");
  10.    links[i].setAttribute("href", "http://localhost:12060/Integrated4.aspx?url="+curlink);
  11. }
  12. }
  13. //--></script>
  14.  
  15. <A href="http://www.softwarecommitments.com">softwarecommitments</A>  
  16.  
  17. </body>
  18.  
  19. </html>
  20.  
May 21 '07 #5
iam_clint
1,208 Expert 1GB
Hi you forgot your onload this time.
Expand|Select|Wrap|Line Numbers
  1. <body onload="updateLinks()">

Man your having a lot of problems :P just kidding let me know if you got it now.
May 21 '07 #6
MrTom
4
Hi Clint,

That's it!

This is a huge improvement for our UI for our demo on Wednesday.

I'm a hero because of your help.

Thank you so much!
-Tom
May 21 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Shapper | last post by:
Hello, I am working on an ASP.NET/VB web site. I have several links including menu links. Considerer a want to load a page named page.aspx. I can do it using javascript. Or using this code:...
8
by: Taras_96 | last post by:
Hi everyone, We' ve come to the conclusion that we wish the user to be directed to an error page if javascript is disabled <enter comment about how a webpage shouldn't rely on javascript here :)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.