Error when using js in href ie 11

2019-02-13 15:48发布

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?

2条回答
够拽才男人
2楼-- · 2019-02-13 16:22

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.

查看更多
家丑人穷心不美
3楼-- · 2019-02-13 16:23

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.

查看更多
登录 后发表回答