12

Is it possible to open a new window or tab via javascript in Windows Phone 7 browser?

window.open does not appear to be supported nor does target='_blank'.

Am I missing something here? This basic feature works just fine on iphone and android. Any ideas on how I can get this working in Windows Phone 7?

bmurmistro
  • 1,070
  • 1
  • 16
  • 35
  • In the browser settings, there's an option to 'Open links in a new tab'. I doubt that can be overriden by the app. – Osiris Sep 04 '12 at 08:17

3 Answers3

28

On Windows Phone 7 this is not possible programmatically. It's all in the users hand.

To cite a Microsoft employee:

"A user can open a link in a new Tab by tapping and holding the link to get the context menu but an anchor or scripts request to target a new window is ignored.

There are several reasons for this:

  • Cross-window communications are not supported.
  • Windows Phone only has one instance of the browser so new "windows" have to be opened as Tab's.
  • The browser experience is full screen so the user has no good visual cue that they have moved to a new Tab unless they explicity request it.
  • Navigating "back" in a new Tab exits the browser which would be confusing to the user if they did not know a new Tab was created."
Community
  • 1
  • 1
acme
  • 14,654
  • 7
  • 75
  • 109
3

If you are trying to add this feature for in-ap browser control, I can suggest you one way.

You have to inject a java-script on every webpage the browser control is able to load the page successfully. In the java-script use window.extern.notify to invoke the ScriptNotify function in your code behind. On the detection of the appropriate message create a new instance of browser control and add it to an array or list. Thereby you can emulate the new tab feature for in-app browser control.

the JS code to be injected may be like this String NEW_TAB_FUNCTION = "window.open = function(__msg){window.external.notify('addnewtab');};";

Which can be injected using browser.InvokeScript("eval", NEW_TAB_FUNCTION);

In ScriptNotify check for addnewtab (keep IsScriptEnabled = True)

    void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        if (e.Value == "addnewtab")
        {   
            //do work here
        }
    }

Note that I have overridden the window.open function in the JS with a function which will be injected every time on a new webpage in order to get notified of user input.

Also note this works only for WebBrowser Control and not external browser.

Milan Aggarwal
  • 5,104
  • 3
  • 25
  • 54
0

My workaround this issue is simple:

window.open

returns null if failed, so in that case I open it in the same window:

var win = window.open(href, '_blank');
    if(!win || win==null){
        window.location.href = href;
    }else{
        win.focus();     
    }

which is a good practice to have a fallback anyway...

Tomer Almog
  • 3,604
  • 3
  • 30
  • 36
  • Hey Tomer, This doesn't work in windows phone 10 Edge browser. Any idea? – Ashutosh Jha Dec 02 '17 at 09:21
  • @AshutoshJha I think it got to do with the window size. Look here: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/11026637/ and here: https://answers.microsoft.com/en-us/insider/forum/insider_internet-insider_spartan-insiderplat_pc/windows-edge-does-not-open-the-new-window-directly/272c607a-253f-47a4-8ec7-4236d30940a1 – Tomer Almog Dec 05 '17 at 05:01