javascript - trying to map someFunction.jQuery to "$" -
i found function (via person's github) might use in script mimics functionality of api object.
here's relevant code link:
unsafewindow = (function() { var e1 = document.createelement('p') e1.setattribute('onclick', 'return window;'); return e1.onclick(); })();
where poster says can use function in format unsafewindow.jquery
now, want able use $
instead of jquery keyword elsewhere in code. tried learning this stack overflow question simplify , re-wrote code so:
(function($){ var e1 = document.createelement('p') e1.setattribute('onclick', 'return window;'); return e1.onclick(); })(jquery);
but didn't work. guess try $ = unsafewindow.jquery
in order map $
, wanted try in format seen above.
you map $
unsafewindow.jquery
so:
unsafewindow = ( function () { var dummyelem = document.createelement('p'); dummyelem.setattribute ('onclick', 'return window;'); return dummyelem.onclick (); } ) (); var $ = unsafewindow.jquery; // can use page's jquery. eg: $("body").append ('<p>content added unsafewindow.jquery</p>');
keep in mind:
this hack, , stop working around chrome version 28.
it may still fail due race condition when userscripts fire. fix that, add
// @run-at document-end
userscript's metadata block.don't things way! cause grief, side effects , maintenance headaches.
for userscripts: use this technique (best cross-browser) or this technique (relies on page's jquery, example shows how use gm_ functions too).
for full extensions or content scripts:, use this technique (use
manifest.json
, keep sandboxed).
Comments
Post a Comment