...

View Full Version : IE error



ninthofjune
07-12-2007, 10:43 AM
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:


if (!document.getElementById||!document.getElementById("quote")) return false;

But this doesn't work. The code is as follows:


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.

_Aerospace_Eng_
07-12-2007, 10:47 AM
Try this

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)

Kor
07-12-2007, 01:51 PM
as far as I know, location is the property of the window object, not of the document object...

ninthofjune
07-12-2007, 02:02 PM
Thanks for your advice, I've amended the script and it now looks like this:


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!

glenngv
07-12-2007, 05:12 PM
Here's how to shorten it:

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)

ninthofjune
07-18-2007, 11:31 AM
Thanks glenngv, script is smaller and all is working well!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum