Search code examples
javascriptdom-events

Stringifyed onClick event


I have an event which parses a certain string which contains button name and URL.

than I assign the data to a button. and add on click event which suppose to open URL in a new window. But this on click event doe not work. The window does not show up. If URL was wrong it would open a window and prompt a mistake.

But in my case when I click the button it does not react. Probably something is wrong with on click event here: onclick=\"myWindow = window.open('partArray[1]', '', 'width=300,height=300');\" . But I can't understand what.

Here is the code:

 <script>
   ...
   var checkType = partArray[0].split("+");
   outPuts = outPuts + "  <input type='button' class='WordDocType' name='" + partArray[0] + "' value='" + partArray[0] + "' onclick=\"myWindow = window.open('partArray[1]', '', 'width=300,height=300');\" /> &nbsp;&nbsp;"
   document.getElementById("demo").innerHTML=outPuts;
  </script>

   <body>
     ...
    <div id="demo"></div>
     ...
   </body>

Solution

  • partArray[1] is not evaluated; the browser should still open window with the URL specified as "partArray[1]" but I doubt that is what you want. Try adding double quotes and concatenating it when you build your HTML.

    outPuts = outPuts + "  <input type='button' class='WordDocType' name='" + partArray[0] + "' value='" + partArray[0] + "' onclick=\"myWindow = window.open('" + partArray[1] + "', '', 'width=300,height=300');\" /> &nbsp;&nbsp;"
    

    If you provide some more context or code it'd be easier to see what might be going on. Are you sure there is not a popup-blocker getting in the way?