Last Updated: October 12, 2018
·
3.682K
· mateuszgachowski

IE bug when changing href with JavaScript

When you attempt to change the href of an anchor in IE (when the text value contains chars that may tell its an anchor, eg. @, http://) the text value will be changed, as well.

Example:

<a href="http://google.com/">http://example.com/</a>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
    $('document').ready(function () {
        $('a').attr('href', 'http://yahoo.com');
    });
</script>

On all browsers just the href changes - anchor text stays the same - example.com.
On IE the anchor text changes for the same value as its href - http://yahoo.com.

To prevent this issue on IE you can try to append invisible HTML entity, such as for example

<wbr> 

to break IE regexp.

Thats all!

1 Response
Add your response

Thank you for your answer @oelmekki!

Maybe you're right, that this is a feature not a bug (:]), but if so - why other browsers don't do that? In fact - when you want to add some malicious code there is no problem to use the workaround that I have mentioned - so thats a poor security feature in my opinion.
And also, as far as I know in IE 10 this does not work anymore.

over 1 year ago ·