This question already has an answer here:
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);
This is a bookmarklet code to inject jquery in any webpage:
Update: I removed the http: part from the URL per @Monkpit comment, which is very important and saves a lot of problems.
Since you are loading jQuery asynchronously, the
jQuery
variable is not available immediately. This means you cannot use jQuery on the next line; you need to wait until the browser loads jQuery and executes it.The solution is to use one of the following techniques:
typeof jQuery === "function"
every x milliseconds)?callback=scriptloaded
, requires server- side support)onload
event as described belowYou forgot a semicolon in row 8. This is the code without errors:
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.
-Lucas