ContextMenu in IE to get the current opened browse

2019-08-20 02:26发布

问题:

I have created an option in IE context menu, so when I open IE, and right click in the window, the added menu is available. Now what I want to do is, I want to get the url of current window. So suppose I open www.facebook.com, then I right click, and choose the additional menu, I want to get the location as www.facebook.com.

How to do this using Javascript? I tried using:

  alert(window.location.href);

However, the location is the location of my test.html file: which has this script:

<html>
alert(window.location.href);
</html>

回答1:

In the htm file, you want to access the external.menuArguments property to access the context from the page you started on (i.e. any selection data or the original dom document object). This should get you the href you're looking for:

external.menuArguments.document.href

Bonus, for selected text in IE 11:

external.menuArguments.document.getSelection().toString()

Edit: also, don't forget to wrap your javascript in a <script></script> tag!