开发者

Dynamic javascript - onclick not visible

开发者 https://www.devze.com 2022-12-19 12:23 出处:网络
I load html & JS code dynamically from webservice. I use pro开发者_Go百科totype update method:

I load html & JS code dynamically from webservice. I use pro开发者_Go百科totype update method:

 placeholder.update(result.Static.html); // which also include javascript code

Immediately after i loaded code, everything works fine:

ProRegResetForm();
alert('reset done');

however, from control declared as

<a href="javascript://" onclick="javascript:ProRegResetForm();" class="au">Reset</a>

I am having error: ProRegResetForm is not defined.

SIMPLIFIED TEST CODE (which is also not working):

<script type="text/javascript">

    var defkeyword_ZipCode = 'Zip Code';

</script>
<a href="javascript://" onclick="alert(defkeyword_ZipCode);">test link</a>


It was correct to write code like:

<a href="javascript://" id="element">test link</a>
<script type="text/javascript">
    functoin somethingcomplex() {
bla bla bla
}

    $('element').onclick = function() { somethingcomplex(); }

</script>


Given that your 'simplified test code' did not execute, you likely have a javascript error elsewhere in your code. Javascript code that follows an exception will not execute.

Have you tried examining the script prior to your snippet (as it appears on the client) using Firebug or IE Developer Toolbar?


I had a similar problem and solved it by delimiting the HTML response from the JavaScript with a '^' and inserting them separately.

//=============================================================================
function injectJavascript(src)
    {
    var scripts = document.getElementById("scripts");
    var javascriptSrc = document.createElement("script");
    javascriptSrc.setAttribute("type", "text/javascript");
    javascriptSrc.setAttribute("language", "JavaScript1.2");
    javascriptSrc.innerHTML = src;
    scripts.appendChild(javascriptSrc);
    }
//=============================================================================
function insertHtmlAndScript(target)
    {
    if (req.readyState == 4) // 4 == "loaded"
        {
        if (req.status == 200) // 200 == "Ok"
            {
            var resp = req.responseText.split("^");
            div = document.getElementById(target);
            div.innerHTML = resp[0];
            injectJavascript(resp[1]);
            }
        else
            {
            alert("Could not retreive URL:\n" + req.statusText);
            }
        }
    }
//======================================================================

The only reason I can think of that this works while your injection does not is that the HTML vs. JavaScript interpretation is threaded such that the JavaScript can not bind properly... but that's just a guess.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号