views:

1113

answers:

5

Hi I am currently trying to replace a query string value in a a link's src attribute.

it works fine in firefox but not in ie.

example:

<a id="link" href="#" src="http://somedomain.com?id=123&amp;size=20"&gt;link&lt;/a&gt;

then on my js it looks kinda like this:

var link = document.getElementById('link');
link.src.replace('size=20', 'size=45');

in ie, it returns something like src is not an object error;

anyone kind enough to lend a hand?

also, i need this to be on native javascript so please don't suggest a framework as a solution thanks.

+1  A: 

To get it to work in IE you're going to need to use link.setAttribute('src', ...).

chaos
A: 

I believe getAttribute is more cross-browser friendly.

var link = document.getElementById('link');
var result = link.getAttribute("src").replace('size=20', 'size=45');

Also, the replace function returns a string. It doesn't operate on the string it is called against. This means you have to assign the result.

link.setAttribute("src", result);
Joel Potter
A: 

use:

var link = document.getElementById('link');
var src = link.getAttribute("src").replace('size=20', 'size=45');
link.setAttribute("src", src);
Jonathan Fingland
+1  A: 

Well, links (anchor elements) don't have a src attribute, I think that you want to change the href attribute:

var link = document.getElementById('link');
link.href = link.href.replace('size=20', 'size=45');
CMS
Good point. Though he could be using src as a custom attribute, in which case setAttribute and getAttribute come back into play.
Joel Potter
well yeah sorry bout that, i used a wrong example (im really fussed lol)... in my real case, its an img tag... im trying to display gravatar images... on preview i use smaller thumbnails hence the s=18 query string. but on click, i want to replace the s=18 with s=45 and place the result on a textbox.
A: 
coderjoe