Search code examples
urlbatch-filebrowsertarget

Open URL with a set Target


EDIT: The solution I am seeking is a command line to be run to accomplish this from a batch file in Windows.


How would I mimic a browser function to open a URL with a specific target so that if that tab is already open it will load there instead of creating a new tab?

So instead of the default http://url.com with the target "_blank", I could change this to "w00t", etc.

I am using Chrome exclusively, so if this were to be a Chrome specific command, that would be acceptable.


Solution

  • You could create a HTML-page and add the following code like this:

    <script>
      var url = getQueryVariable("url");
      var target = getQueryVariable("target");
      window.open(url,target);
    
      function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
          if (pair[0] == variable) {
            return pair[1];
          }
        }
      }
    </script>
    

    I have this script hosted here: scriptcoded.github.io/redirect

    You could then call it from a batch-file using this command:

    START scriptcoded.github.io/redirect?url=http://www.google.com&target=w00t
    

    Use ?url=http://www.google.com&target=_blank to set the redirect.

    The only problems I find with this method is that some browsers blocks the new window and that the tab won't close. Yes, we could use window.close();, but it will only close tabs that were opened by the webpage itself.

    Hope it helps!