3

I would like to know is it possible to save some, for example,simplemath.js file with

function ADD(a, b)
{
   return a + b;
}

simple function, run opera's or some other browser's javascript console, include somehow this (simplemath.js) file, call ADD(2, 5), and get a result in console or execute javascript code on current web page and manipulate with it's content. How can I do that? How can I use javascript functions from external files in web-browser's javascript console?

// EDITS

simplemath.js is just local file, somewhere on hard drive (c:\temp\simplemath.js), no localhost web servers running. I want to run scripts from it on any web page and get result similar to typing javascript: function ADD(a, b){return a+b;} alert(ADD(1, 41);) in browsers address bar, but via console, and load ADD function from external file.

2
  • Where is the simplemath.js file located? Somewhere on the web, or on your disk? Do you have a webserver running on your localhost? Commented Nov 22, 2012 at 14:53
  • It's just local file, somewhere on hard drive (c:\temp\simplemath.js), no web servers running. I want to run scripts from it on any web page and get result similar to typing javascript: function ADD(a, b){return a+b;} alert(ADD(1, 41);) for example in browsers address bar, but from console, and load this function from external file. Commented Nov 22, 2012 at 14:59

2 Answers 2

3

You have at least two ways too load the script in the header from the console:

document.head.innerHTML+="<script src='simplemath.js'></script>";

or using appendChild():

var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'simplemath.js';
document.head.appendChild(script);

if the ADD function is defined in the global space you can call ADD(2, 5) directly in the console.

Also you can load jQuery using the above ways first and then load the scripts with:

$.getScript('simplemath.js');

And most importantly so you don't have any problem with security or paths and be allowed to load those javascript files you should have all those scripts in one folder in the same folder an empty html and open the console in the same window that empty html is ran. Else you will have problems with domains and paths.

Sign up to request clarification or add additional context in comments.

4 Comments

But what if I want to run those scripts on external web pages, not on my local??? On google page, for example.
@DmytroTsiniavsky That is against the security policy, you are not allowed to load JavaScript files from another domain, but you can do that in chrome for example by disabling security running chrome with command line option --disable-web-security and then specifying on src the entire path to the js file preceded by file://.
Aren't there any other way to use *.js file as some kind of a library or extension only for console? Without injecting it to html web page?
@DmytroTsiniavsky Not that I know of, only this much I could help.
2

You can include your simplemath.js file as a userscript into Opera:

If enabled, User JavaScript will be loaded on most pages that you visit, including pages in any frames and inline frames. Any global functions and variables created in the User JavaScript will be available, and can be read by any scripts on these pages. For this reason, to protect your privacy and security, we recommend that you do not include any sensitive information in your User JavaScript

You can enable it globally or on specific pages only. Opera does also support greasemonkey scripts, a Firefox addon. Yet (at least in FF) they usually run in a sandbox, so they will not leak global variables into the webpage where it would be accessible from the console (unless you explicitly state so).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.