locked
wondow.open url cannot add document.getElementById value RRS feed

  • Question

  • User-775831949 posted

      <input id="UserName" type="text" class="UserName" name="UserName">

     <select >
                                            <option value="google" callback="google">Google</option>

    </select>

    <script>
    function google() {
                window.open('http://localhost/ReceivePage.aspx?Name=')
            };

    Works

    However, I need to add the value from input box to the url so I use

    function google() {
                window.open('http://localhost/ReceivePage.aspx?Name=' + document.getElementById('UserName').value)
            };

    Then it does not open any page.
    Please help. What am I missing ?

    Thanks

    Sunday, December 10, 2017 8:16 AM

Answers

  • User-1838255255 posted

    Hi hkbeer,

    According your code and needs, I make a modify based on your code, please check:

    Sample Code:

    <head>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <input id="UserName" type="text" class="UserName" name="UserName">
        <select>
            <option value="google" callback="google">Google</option>
        </select>
        <input id="Button1" type="button" value="Tri" onclick="return google()" />
        <script>
            function google() {
                window.open('http://localhost:52021/ReceivePage.aspx?Name=' + document.getElementById('UserName').value)
            };
        </script>
    </body>

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 9:23 AM

All replies

  • User475983607 posted

    I don't see anything wrong with the JavaScript other than a misplaced semicolon but that would not cause the issue described.  Also, your calling localhost unless you app is running on port 80 you should either have a port number in the address (IIS Express) or the app name in the URL (IIS localhost).  If this is on a production site then the URL is incorrect.

    For example this works as expected and opens a new tab when the button is clicked.

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Default</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
        <script src="Scripts/jquery-ui-1.12.1.js"></script>
        <script type="text/javascript">
            function google() {
                window.open('http://localhost:51867/basic.html?Name=' + document.getElementById('UserName').value);
            }
        </script>
    
    </head>
    <body>
        <input id="UserName" type="text" class="UserName" name="UserName">
        <input id="Button1" type="button" value="button" onclick="google()" />
    </body>
    </html>
     

    The first step is open Dev Tools and look for any errors.  The next step is try opening the ReceivePage.aspx?Name=testdata using your browser's address bar.  Use The Visual Studio debugger to set a breakpoint in ReceivePage.aspx and verify the page is being hit and there are no errors related to the querystring value.

    Lastly, it seems that you are using a 3rd party API, callback attribute, as there is nothing in the posted code that show how the goole() function is called.  When asking for help it is important to include this type of information.

    Sunday, December 10, 2017 12:53 PM
  • User-1838255255 posted

    Hi hkbeer,

    According your code and needs, I make a modify based on your code, please check:

    Sample Code:

    <head>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <input id="UserName" type="text" class="UserName" name="UserName">
        <select>
            <option value="google" callback="google">Google</option>
        </select>
        <input id="Button1" type="button" value="Tri" onclick="return google()" />
        <script>
            function google() {
                window.open('http://localhost:52021/ReceivePage.aspx?Name=' + document.getElementById('UserName').value)
            };
        </script>
    </body>

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 9:23 AM