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:

  1. this hack, , stop working around chrome version 28.

  2. it may still fail due race condition when userscripts fire. fix that, add // @run-at document-end userscript's metadata block.

  3. 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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -