Note that there are some explanatory texts on larger screens.

plurals
  1. POInjecting code into the web page does not work
    text
    copied!<p>This is a follow-up to a <a href="https://stackoverflow.com/questions/5574231/how-to-dynamically-inject-a-javascript-function">question</a> I asked yesterday.</p> <p>I have a userscript (kind of like GreaseMonkey script, but for Chrome). </p> <p>The idea is to add a textbox and a button to the page. Then when the user clicks the button, it kicks off a function that does stuff. So I inject the textbox, button and the function into the page, but when the user clicks the button, the Chrome console tells me "Uncaught TypeError: object is not a function". So obviously it does not see the function I just injected and that is specified in the <code>onclick</code> event for the button.</p> <p>So I have code like this:</p> <pre><code>initialize(); function initialize() { var dropDown = document.getElementById("tstGlobalNavigation_ddlChooseProject"); // add a textbox dropDown.innerHTML = dropDown.innerHTML + "&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input type='text' name='txtSearch' style='position:absolute;top:8px;left:800px;width:50px' &gt;"; // add a button dropDown.innerHTML = dropDown.innerHTML + "&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input type='button' name='btnSearch' value='Go' onclick='fn()' style='position:absolute;top:8px;left:860px;width:35px'&gt;"; addScript("var obj = document.getElementById('txtSearch'); " + "if (obj != null) { " + " var incidentId = document.getElementById('txtSearch').value; " + " var currentURL = location.href; " + " var splitResult = currentURL.split('/'); " + " var projectId = splitResult[4]; " + " location.href = 'http://dev.myApp.com/ProductTeam/' + projectId + '/Incident/' + incidentId + '.aspx'; " + " }" , "fn"); } function addScript(contents, id) { var head, script; head = document.getElementsByTagName('head')[0]; script = document.getElementById(id); if(script != undefined) { head.removeChild(script); } script = document.createElement('script'); script.type = 'text/javascript'; script.id = id; script.innerHTML = contents; head.appendChild(script); } </code></pre> <p>What am I missing here? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload