<a href='http://www.domain.com' id='replace' style='text-decoration:none;color:black;font-size:10px;'>This is text link</a>
<script language="javascript">
var newURL = "mydomain.com/?refid=4877";
onload=function() {
var dt = document.getElementById("replace");
document.body.innerHTML = dt.getAttributeNode("href").value.replace(/domain.com/g,newURL);
}
views:
50answers:
2
A:
You should probably use JQuery:
$(document).ready(function(){
$("#replace").attr("href","http://www.mydomain.com/?refid=4877");
});
Jieren
2010-01-14 06:48:53
-1, for telling the user he should use jQuery for such a simple task. If I had asked the question, I definitely wouldn't appreciate being told I "should probably" use jQuery just for replacing a simple string on a dom element's property. jQuery isn't needed for this task. It would also require extra time to learn jQuery if the asker has never used it before.
Andy E
2010-01-14 09:12:46
Ah sorry about this. I'm still kind of unfamiliar with StackOverflow etiquette, but I guess when you have a damn good hammer, everything starts looking like a nail :-)
Jieren
2010-01-14 18:21:18
+2
A:
Just assign to the href attribute:
dt.href = dt.href.replace(/domain.com\/?/, newURL);
The optional trailing slash caters for browsers that automatically add a slash to hrefs.
harto
2010-01-14 06:52:43
it work, but there some slash "/" at the end.http://www.domain.com/?refid=4877/
Agus Puryanto
2010-01-14 09:57:59
Looks like a slash is appended to the original href by the browser. I'll tweak my answer
harto
2010-01-14 22:42:03