Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject jquery to any webpage [duplicate]

Is there any way to inject jQuery into any page as we do with javascript(from url). with javascript we do this

javascript:alert("b"); 

I tried this but I don't know why it dosen't work

javascript:var x = document.getElementsByTagName("head")[0]; var y = document.createElement("script"); y.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"; x.appendChild(y);  var a = document.getElementsByTagName("body")[0]; var b = document.createElement("script"); b.innerHTML = "$('p').css('border','3px solid red')" a.appendChild(b); 
like image 475
elti musa Avatar asked Oct 26 '14 12:10

elti musa


People also ask

How to inject jQuery into page?

You can inject jQuery in Chrome by putting it as a bookmark. Just copy the code above, create a new bookmark of a random website. Right click on the bookmark and choose 'Edit', paste the code in the URL box and choose 'Save'. When you click on the bookmark the jQuery script will be injected.

How add jQuery library to HTML?

Step 1: Firstly, we have to open that Html file in which we want to add the jQuery using CDN. Step 2: After then, we have to place the cursor between the head tag just before the title tag. And, then we have to use the <script> tag, which specify the src attribute for adding.

Where should I inject jQuery in HTML?

jQuery is embedded into the <script> tag of HTML file between the <head> tag and the <title> tag.


1 Answers

This is a bookmarklet code to inject jquery in any webpage:

javascript: (function (){     function l(u, i) {         var d = document;         if (!d.getElementById(i)) {             var s = d.createElement('script');             s.src = u;             s.id = i;             d.body.appendChild(s);         }     } l('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', 'jquery') })(); 

Update: I removed the http: part from the URL per @Monkpit comment, which is very important and saves a lot of problems.

like image 85
Amr Elgarhy Avatar answered Sep 17 '22 15:09

Amr Elgarhy