www.webdeveloper.com
Results 1 to 2 of 2

Thread: Open concatenated string in new tab using javascript

Hybrid View

  1. #1
    Join Date
    May 2014
    Posts
    1

    Open concatenated string in new tab using javascript

    I am creating a webpage, and I want to add a functionality to it: I need to concatenate 2 strings using javascript. The first string is an input from a blank box (in which I can copy a link), and the second string is a constant string.

    On the click of a button on the webpage, the concatenated string (which will be a web-address) should open in a new tab or window. I am trying with the following code:

    <!DOCTYPE html>
    <html>
    <body>

    <p>Click the button to join three strings into one new string.</p>

    <button onclick="myFunction()">Go</button>

    <p id="demo"></p>

    <script>
    function myFunction() {
    var str1 = "www. ";
    var str2 = "google";
    var str3 = ".com";
    var res = str1.concat(str2,str3);
    document.getElementById("demo").innerHTML = res;
    }
    </script>

    </body>

    </html>
    But instead of writing the result in the webpage itself, the Go button should open the link in a new tab. Also, instead of defining the string "www." in the code itself, I want to be able to enter it interactively by writing it in a blank box, but I am not getting how to code this.

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    I did a simple test to see if I could actually do this (note: the user must have open in tab instead of new window configured in their browser).
    Code:
    function pseudoOpen(url)
    {
        var node = document.createElement('a');
        node.href = url;
        node.target = '_blank';
        node = document.body.appendChild(node);
        node.click();
        // clean up...
        document.body.removeChild(node);
        node = null;
    }
    This works also (same rules applies)
    Code:
    window.open(url, '_blank');
    Last edited by ShrineDesigns; 05-24-2014 at 05:08 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles