Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using js in href ie 11

I have a link:

<a href="javascript:someObject.someFunction();" target="_blank" style="color: rgb(225, 233, 41);">someText</a>

it works fine everywhere except ie(i try ie11) i have this error

This page can’t be displayed. 
Make sure the web address //ieframe.dll/dnserror.htm# is correct.

How can i solve this?

like image 340
Suhan Avatar asked Apr 15 '14 07:04

Suhan


2 Answers

If you use a javascript URI scheme in a HTML href attribute, this is different to using an onclick event handler.

In IE, the result of executing that JavaScript will replace the currently loaded document.

To avoid this (without refactoring your code to not do things this way), you can end your href with the javascript operator void, which tells your javascript to return nothing, at all (well, undefined).

Then IE will stay on the current page.

<a href="javascript:someObject.someFunction(); void 0" ...

...and you probably don't want the target="_blank" since you're telling a new window to run your JavaScript code, and your function is not available in that window.

like image 112
Lee Kowalkowski Avatar answered Nov 15 '22 02:11

Lee Kowalkowski


I would do this instead:

<a href="#" onclick="event.preventDefault(); someObject.someFunction();" target="_blank" style="color: rgb(225, 233, 41);">someText</a>

It will open a new tab as you intended, and it works in chrome, firefox and IE.

like image 3
Chris Ji Avatar answered Nov 15 '22 02:11

Chris Ji