load JS on demand

Forums » General Opera topics » User JavaScript

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

1. June 2010, 02:05:27

epilos

Posts: 2044

load JS on demand

hello

I have a JS that I want it to load when I click a button or something and not in every webpage I visit

how can I do that?

thanks
Opera 11 on WinXP Pro SP3 • AMD hexacore 3.2Ghz with 4Gb RAM

3. June 2010, 08:15:19

nowotny

Posts: 1296

Make a bookmarklet...? :/ This article shows how to effectively load jQuery with a bookmarklet... Instead of jQuery you can load your script...

OR

Make a function out of your JS, put it into a UserJS and then use a bookmarklet to activate your function...

3. June 2010, 17:10:08

epilos

Posts: 2044

wow there is alot of code there, I am not familiar with
isn't it a simple procedure? if yes, can you give an example?
thanks
Opera 11 on WinXP Pro SP3 • AMD hexacore 3.2Ghz with 4Gb RAM

6. June 2010, 10:12:48

dertbox

Posts: 832

Make another folder for the website you don't want to run the script on and put all but the scripts you don't want into it, then set that as the userjs folder in the site preferences.

8. June 2010, 14:05:05 (edited)

epilos

Posts: 2044

Originally posted by dertbox:

Make another folder for the website you don't want to run the script on and put all but the scripts you don't want into it, then set that as the userjs folder in the site preferences.



there are numerous websites I want to run the script and numerous I don't want to run it, so doing this on per website basis is very time consuming
Opera 11 on WinXP Pro SP3 • AMD hexacore 3.2Ghz with 4Gb RAM

8. June 2010, 13:20:21

dertbox

Posts: 832

Any specific examples?

If you want to something do this, you have to be prepared to invest a little time into it.

8. June 2010, 13:34:00

Lex1

Posts: 1718

I want to run the script


// script
window.addEventListener('message', function(e){
	if(e.data == 'prefix'+'_run'){
		runScript();
		// frame support
		var postMsg = function(msg){for(var i = 0, f = window.frames, l = f.length; i < l; i++)f[i].postMessage(msg)};
		postMsg('prefix'+'_run');
	};

}, false);



// button
Go to page, "javascript:window.postMessage('prefix'+'_run')"

8. June 2010, 14:05:55

epilos

Posts: 2044

Originally posted by Lex1:

I want to run the script


// script
window.addEventListener('message', function(e){
	if(e.data == 'prefix'+'_run'){
		runScript();
		// frame support
		var postMsg = function(msg){for(var i = 0, f = window.frames, l = f.length; i < l; i++)f.postMessage(msg)};
postMsg('prefix'+'_run');
};

}, false);



// button
Go to page, "javascript:window.postMessage('prefix'+'_run')"



Lex1 thanks but where do I put the code of the JS I want to run ??
Opera 11 on WinXP Pro SP3 • AMD hexacore 3.2Ghz with 4Gb RAM

14. June 2010, 11:49:39

Lex1

Posts: 1718

For compatibility with latest build:

// script
window.addEventListener('message', function(e){
	if(e.data == 'prefix'+'_run'){
		runScript();
		// frame support
		var postMsg = function(msg){for(var i = 0, f = window.frames, l = f.length; i < l; i++)f[i].postMessage(msg, '*')};
		postMsg('prefix'+'_run');
	};

}, false);



// button
Go to page, "javascript:window.postMessage('prefix'+'_run', '*')"

14. June 2010, 18:31:26

QuHno

read a book!

Posts: 1037

Originally posted by Lex1:

For compatibility with latest build:


Thank you! Thank you! Thank you!
You saved my day!
Looking for a new home for your blog, albums, mail and forums after my.opera closes at march 1, 2014?

Visit https://vivaldi.net - the new community set up by Jon S. v. Tetzchner and several former Opera employees. Many of us are already there and some of the employees too smile

14. June 2010, 22:18:18

epilos

Posts: 2044

Originally posted by QuHno:

Thank you! Thank you! Thank you!
You saved my day!



what I must do exactly for this to work?

confused
Opera 11 on WinXP Pro SP3 • AMD hexacore 3.2Ghz with 4Gb RAM

15. June 2010, 04:22:57

QuHno

read a book!

Posts: 1037

Edit YOUR_BUTTON_TITLE to the title and YOUR_SKIN_IMAGE to the icon name you want to use for your button. Replace spaces in title and name with %20
opera:/button/Go%20to%20page%2C%20%22javascript%3Awindow.postMessage('prefix'%2B'_run'%2C'*')%3B%22%2C%2C%22YOUR_BUTTON_TITLE%22%2C%22YOUR_SKIN_IMAGE%22

  1. Save it as HREF in an A tag on a webpage or in the URL Tag here in the forums or your blog
  2. Click on it
  3. Click "OK"
  4. Drag it to the toolbar you want

Example with the code above: YOUR BUTTON TITLE

or

paste the code into the adressbar and press the "Enter" key. You'll see an error page with an url in it, click on it and proceed as in Step 3 above above.

For the Javascript:
Copy the code that Lex1 posted and replace runScript(); with your code.

Example:
// script
window.addEventListener('message', function(e){
	if(e.data == 'prefix'+'_run'){

// Paste your working script code below ********
function(){
	alert('It works!');
}
// End of your code ***********

		// frame support
		var postMsg = function(msg){for(var i = 0, f = window.frames, l = f.length; i < l; i++)f[i].postMessage(msg, '*')};
		postMsg('prefix'+'_run');
	};

}, false);
Looking for a new home for your blog, albums, mail and forums after my.opera closes at march 1, 2014?

Visit https://vivaldi.net - the new community set up by Jon S. v. Tetzchner and several former Opera employees. Many of us are already there and some of the employees too smile

Forums » General Opera topics » User JavaScript