Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    please help make this code open in new tab

    just started learning scripting.

    This bookmarklet essentially takes the domain name of the tab currently open when one pressses it & runs a search query through Google prefixing the related: search operator.

    As evident, here it shows the result in the same page replacing the earlier one. I would like it to open in a new tab. Can you please tell me how to do so. Self-learning it. At an initial stage of learning, I am.

    Code:
    javascript:location.href='https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host)

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Whether a page opens in a new window or a new tab is dependent on the user's browser settings. You cannot control that.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Whether a page opens in a new window or a new tab is dependent on the user's browser settings. You cannot control that.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    can i at least instruct it to open in a new tab, if the user's browser permits it.

    This code, currently, won't so even if the browser settings permits it

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    878
    Thanks
    0
    Thanked 114 Times in 113 Posts
    Code:
    javascript:window.open('https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host),'_blank');
    If it opens in a tab, a window, or opens at all, depends on users browser settings

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by lerura View Post
    Code:
    javascript:window.open('https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host),'_blank');
    If it opens in a tab, a window, or opens at all, depends on users browser settings
    Hi,
    Thanks. While it does open up the result in a new tab, it also changes the original page.
    On the page [not the result page], it reads
    [object Window]
    and in the address bar
    Code:
    javascript:window.open('https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host),'_blank');
    i have to press the back key to go back. I would like the initial page to remain unchanged, only the result must appear in a new table

  • #6
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    878
    Thanks
    0
    Thanked 114 Times in 113 Posts
    Quote Originally Posted by jonopa View Post
    Hi,
    Thanks. While it does open up the result in a new tab, it also changes the original page.
    On the page [not the result page], it reads

    and in the address bar
    Code:
    javascript:window.open('https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host),'_blank');
    i have to press the back key to go back. I would like the initial page to remain unchanged, only the result must appear in a new table
    Sorry! It is on me!
    Use this instead:
    Code:
    <a href="void()" onclick="window.open('https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host),'_blank'); return false">LinkText</a>

  • #7
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by lerura View Post
    Sorry! It is on me!
    Use this instead:
    Code:
    <a href="void()" onclick="window.open('https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host),'_blank'); return false">LinkText</a>
    nopes, not working. this one doesn't do anything. the javascript is to be used in the browser toolbar

  • #8
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    878
    Thanks
    0
    Thanked 114 Times in 113 Posts
    This should work then:
    Code:
    javascript:newwin=function(){window.open('https://www.google.com/search?q=related%3A'+encodeURIComponent(location.host),'_blank');};newwin;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •