1

I cannot figure how to get my JS to open the URL in another tab.

Here’s the script

function viewSource(){
   var webcache = "http://webcache.googleusercontent.com/search?q=cache:";
   var request = "&prmd=ivn&strip=0&vwsrc=1";
   var url = window.prompt("Enter valid URL");
   window.location.href = webcache + url + request;
}

viewSource();

I tried adding ’_blank’ in the code, but I couldn’t figure where to place it.

Maybe it can only be achieved with window.open(url)?

Edit: I forgot to mention that it must be suitable for Safari browsers, see window.open(url, '_blank'); not working on iMac/Safari.

Chasidish
  • 57
  • 6
  • 1
    Does this answer your question? [JavaScript: location.href to open in new window/tab?](https://stackoverflow.com/questions/5141910/javascript-location-href-to-open-in-new-window-tab) – StudioTime Jan 22 '20 at 09:12
  • Sadly it didn’t, I couldn’t couldn’t figure out where to properly place the `_blank` string. – Chasidish Jan 22 '20 at 09:15

3 Answers3

3

To open a new window/tab you should use window.open() just like that:

function viewSource(){
  var webcache = "http://webcache.googleusercontent.com/search?q=cache:";
  var request = "&prmd=ivn&strip=0&vwsrc=1";
  var url = window.prompt("Enter valid URL");
  
  const Link=webcache+url+request;
  
  //Check for myService object (Refers to iMac/Safari)
  if(typeof myService!="undefined"&&typeof myService.getUrl!="undefined"){
    const windowReference = window.open();
    myService.getUrl().then(function(url) {
         windowReference.location = Link;
    });
  //Otherwise, we can just open a normal window ( Chrome, Firefox, ..)
  }else window.open(Link, '_blank');
  return Link;
}
<button onclick="viewSource();">Open Link</button>
Adnane Ar
  • 683
  • 7
  • 11
  • it's simple you are just going to remove the html part and call the `viewSource()` function just like you did in your script, Hope it helps you! – Adnane Ar Jan 22 '20 at 09:28
  • This is the only way `window.open` can be achieved in Safari! See [window.open(url, '_blank'); not working on iMac/Safari](https://stackoverflow.com/questions/20696041/window-openurl-blank-not-working-on-imac-safari) – Chasidish Jan 22 '20 at 09:31
  • To use window.open() on iMac/Safari we are going to need the button! unfortunately – Adnane Ar Jan 22 '20 at 09:33
  • Hello, again I made some modifications after a research and I've made this script which I think it would work on iMac/Safari too! – Adnane Ar Jan 22 '20 at 09:43
1

Try this.

function viewSource() {
    var webcache = "http://webcache.googleusercontent.com/search?q=cache:";
    var request = "&prmd=ivn&strip=0&vwsrc=1";
    var url = window.prompt("Enter valid URL");
    if (url && url.match(/^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/g)) {

        window.open(
            webcache + url + request,
            '_blank'
        );
    } else {
        alert('Invalid url, Please enter a valid url');
        viewSource();
    }

}
viewSource()
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
  • 1
    I guess you forgot to call `viewSource()` after the `alert('Invalid url, Please...')`, just so the user could have another chance to entrer a new url! – Adnane Ar Jan 22 '20 at 09:30
0

you need to replace window.location.href = webcache + url + request; with following line

window.open(webcache + url + request,
      '_blank' // <- This is what makes it open in a new window.
      );
Yogesh.Kathayat
  • 974
  • 7
  • 21