Is there any way to load css and javascript from a string?

后端 未结 3 1004
我寻月下人不归
我寻月下人不归 2021-01-06 10:30

I\'ve seen many examples of loading CSS and javascript dynamically from a file. Here\'s a good example. But is there a way to load CSS or javascript as a string? For exam

3条回答
  •  清歌不尽
    2021-01-06 11:05

    var javascript = "alert('hello');";
    eval(javascript);
    

    This will load the JavaScript from a string, but be warned that this is highly insecure and not recommended. If a malicious process ever interfered with your string of JavaScript, it could result in security issues.

    As far as CSS goes, you can append a style tag to the page using jQuery, like so:

    $('head').append("");
    

    Or, for the JavaScript purists:

    var s = document.createElement("style");
    s.innerHTML = "body { background-color:white !important; }";
    document.getElementsByTagName("head")[0].appendChild(s);
    

提交回复
热议问题