Download jQuery plugin from String

I want to do something a little hacky.

Say I have jQuery plugin code in this variable:

var script = '!function( some jQuery plugin )(window.jQuery)';

I want to download it. I tried eval (script) and jQuery.globalEval (script). This does not work. If I had this pluggable code in a file, I could do jQuery.getScript (url, callback). But I only have code in the line. I was thinking about loading this line into the cache associated with the mock URL, but I don't know if this is possible.

Any suggestions? ... other than "don't do this." I know that the "eval is evil" mantra, but think of it as an academic matter.

Key code features in a script variable:

Prologue:

!function(a){"use strict";

Epilogue:

gister(b)})}}(window.jQuery||window.Zepto);
+4
source share
1

. eval , . a Function() . , .


. , URL- ( , ). , :

var scriptData = "$.fn.jQueryExtension = function(){ this.each(function(){/*code*/}); };";

. :

window.location = "javascript:"+scriptData;

.html()

jQuery .html(), . script. html- script, ajax ( , - html). , scriptData.

var scriptData = "$.fn.jQueryExtension = function(){ this.each(function(){/*code*/}); };";

script.

var scriptHtml = "<script>"+scriptData+"</script>";

html dom.

var scriptDiv = $("<div>").html(scriptHtml);
$("body").append(scriptDiv);

, , - .

var scriptData = "alert('test');";
var scriptHtml = "<script>"+scriptData+"</script>";
var scriptDiv = $("<div>").html(scriptHtml);
$("body").append(scriptDiv);
+3

Source: https://habr.com/ru/post/1528202/


All Articles