www.webdeveloper.com
Results 1 to 4 of 4

Thread: Dynamic Link Replacement?

  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Dynamic Link Replacement?

    I am in the process of building a web page and I am trying to use JS to verify URL's via CSS id's.

    I am trying to do something along these lines:

    Add link:
    HTML Code:
    <a id="verifylink" href="http://www.example.com">Link</a>
    Then use Javascript to pull that link, make sure it equals example.com and if it doesn't, change it to example.com.

    I thought I could add a external js file with the JS function init() in <head>, then run <body onLoad="init()"> to go through the page and check them all. I can't get it to work.

    I was using this code (inside the init() function):

    Code:
    if(document.getElementById('verifylink').href != "http://www.example.com") 
    {
    document.getElementById('verifylink').href = "http://www.example.com";
    }
    Any help in getting this working would be helpful. Let me know if you need any more info.

    Thanks!

  2. #2
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    Works for me. What browser are you having this issue in? Sure you're not trying to change the href before the document has finished loading?

  3. #3
    Join Date
    Nov 2009
    Posts
    2
    I am having problems in all browsers (IE, FF, Chrome). I have cleared the cache/cookies/etc from all of them.

    I would have this as my JS (external.js):

    Code:
    function init() {
    
    if(document.getElementById('verifylink').href != "http://www.example.com") 
    {
    document.getElementById('verifylink').href = "http://www.example.com";
    }
    
    }
    Then my html would be:

    HTML Code:
    <head> 
     
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Site Title</title> 
    <link href="style.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="external.js"></script> 
    </head> 
     
    <body onLoad="init()">
    <a id="verifylink" href="http://not.example.com">link</a>
    </body>
    I do have multiple sets of links that I am trying to change (i.e verifylink, verifylink2) I am just copy and pasted the if statement below the original and changed the id name to verifylink2. The second if statement still resides within the init() function.

    Thanks!

  4. #4
    Join Date
    Dec 2008
    Posts
    488
    You can't set the HREF attribute directly for IE. You should change this to document.getElementById('verifylink').setAttribute('href','http://not.example.com');

    Also, why even check what the HREF contains, if you always and definitely want it to be the same thing? Just set it, regardless of what it equals. You could also loop through all links on the page and use RegExp to adjust them accordingly, but we need to determine the criteria for the change... for example, are you trying to just make sure they all have "www" instead of a sub-domain? If you do this correctly, you could use one short function to adjust every link on the page.

    ONE MORE THING:

    I noticed you're not declaring a DOCTYPE. This is very important for determining how the browser views scripts. You are using XHTML transitional style code, so you need to use this DOCTYPE and <html> tag:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    And your onLoad attribute needs to be lower case: <body onload=".....">
    Last edited by jamesbcox1980; 11-09-2009 at 11:02 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles