Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: IE error

  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Bristol
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IE error

    I have the following code which I use to grab an ID and attach a link to an element. The code work fine across all browsers. However, with IE, if the ID is not present then it shows an error symbol at the bottom left of the browser window. Is there a way to make IE behave? I've tried adding a check like:

    Code:
    if (!document.getElementById||!document.getElementById("quote")) return false;
    But this doesn't work. The code is as follows:

    Code:
    function addLink(element, url) {
    	element.onmouseover = function () { this.style.cursor = 'pointer' }
    	element.onmouseout = function () { this.style.cursor = 'default' }
    	element.onclick = function() { document.location = url; }
    }
    
    function prepareLinks() {
    	if (!document.getElementById) return false;
    	var title = document.getElementById("title");
    	var quote = document.getElementById("quote");
    	var how = document.getElementById("how");
    	
    	addLink(title, "index.aspx");
    	addLink(quote, "quote.aspx");
    	addLink(how, "how.aspx");
    }
    
    addLoadEvent(prepareLinks)
    This is more an annoyance than anything else, but any help would be appreciated.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this
    Code:
    function addLink(element, url) {
    	element.onmouseover = function () { this.style.cursor = 'pointer' }
    	element.onmouseout = function () { this.style.cursor = 'default' }
    	element.onclick = function() { document.location = url; }
    }
    
    function prepareLinks() {
    	if(document.getElementById("quote"))
    {
    	var title = document.getElementById("title");
    	var quote = document.getElementById("quote");
    	var how = document.getElementById("how");
    	
    	addLink(title, "index.aspx");
    	addLink(quote, "quote.aspx");
    	addLink(how, "how.aspx");
    }
    }
    
    addLoadEvent(prepareLinks)
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    as far as I know, location is the property of the window object, not of the document object...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Bristol
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your advice, I've amended the script and it now looks like this:

    Code:
    function addLink(element, url) {
    	element.onmouseover = function () { this.style.cursor = 'pointer' }
    	element.onmouseout = function () { this.style.cursor = 'default' }
    	element.onclick = function() { window.location = url; }
    }
    
    function prepareLinks() {
    	if (!document.getElementById) return false;
    	var title = document.getElementById("title");
    	addLink(title, "index.aspx");
    	
    	if (document.getElementById("quote")) {
    	var quote = document.getElementById("quote");
    	addLink(quote, "quote.aspx");
    	}
    	
    	if (document.getElementById("how")) {
    	var how = document.getElementById("how");
    	addLink(how, "how.aspx");
    	}
    }
    
    addLoadEvent(prepareLinks)
    Works correctly with no errors, just left with a script that is a lot larger than I would have hoped to carry out such a simple function!

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Here's how to shorten it:
    Code:
    function addLink(element, url) {
    	if (element){
    		element.onmouseover = function () { this.style.cursor = 'pointer' }
    		element.onmouseout = function () { this.style.cursor = 'default' }
    		element.onclick = function() { window.location = url; }
    	}
    }
    
    function prepareLinks() {
    	var title = document.getElementById("title");
    	var quote = document.getElementById("quote");
    	var how = document.getElementById("how");
    	
    	addLink(title, "index.aspx");
    	addLink(quote, "quote.aspx");
    	addLink(how, "how.aspx");
    }
    
    addLoadEvent(prepareLinks)
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New to the CF scene
    Join Date
    Feb 2007
    Location
    Bristol
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks glenngv, script is smaller and all is working well!


  •  

    Posting Permissions

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