1



I want to write a custom jquery function on a file, and then open any website in chrome , click on inspect element, go to console, and there i want to execute my jquery function.

How should i do to call the function from the file instead of writing it directly in the console ?

(assume that the loaded web page is already supporting jquery)

Tourki
  • 1,785
  • 15
  • 22
  • [This](http://stackoverflow.com/a/10612311/1133144) solution may help you since you're using chrome (I didn't test it myself). – ioums Oct 23 '13 at 12:54

2 Answers2

2

Create a bookmarklet of it! Since you can use javascript: in URLs, you can create run-anywhere snippets of code you can call simply by clicking on the bookmark. My favorite pattern would be javascript: (function() { ... })(); Stick the bookmark in your toolbar for easy access.

Nettuts has an excellent how-to on creating a basic bookmarklet.

Brian North
  • 1,398
  • 1
  • 14
  • 19
  • i have another question, is there a way to write to a local file from a jquery function ? i want to write the result of my bookmarklet function to a local file , have you any idea ? thanks again – Tourki Oct 23 '13 at 13:59
  • There is no "pure" JS way to save to the local filesystem. However, there are a few mixed Java/jQuery plugins available, or HTML5's local storage capabilities. This [SO question's answers](http://stackoverflow.com/questions/582268/read-write-to-file-using-jquery) cover pretty much to whole range of options. – Brian North Oct 23 '13 at 22:11
0

if you could host your file somewhere (a local iis or apache might do) then you can add the file to a websites head using something like this in the console

document.getElementsByTagName('head')[0].innerHTML += '<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>';

this example adds the jquery file to the head.

th1rdey3
  • 4,176
  • 7
  • 30
  • 66