0

i am working like bookmark plugin using external js/css files.

 <a class="add-menu-text" href="javascript: (function (5) {   
var jsCode = document.createElement('script'); 
jsCode.setAttribute('src','http://mywebsite.com/javascripts/bookmark.js'); 
document.body.appendChild(jsCode); }());">
 Bookmark 
</a>

But here i want to pass variable and get the variable in external file (bookmark.js).

I drag and drop the "Bookmark" in toolbar. once i click the "Bookmark" in tool bar i need to get the variable in external website.(bookmark.js).

I need to get the value(5) in bookmark.js

1
  • I drag and drop the "Bookmark" in toolbar. once i click the "Bookmark" in tool bar i need to get the variable in external website.(bookmark.js). Commented May 6, 2015 at 7:47

2 Answers 2

1

You can load external script with the jQuery.getScript() function

$.getScript( "bookmark.js", function( data, textStatus, jqxhr ) {
  //Data will contain the returned data of your script
});
Sign up to request clarification or add additional context in comments.

1 Comment

What about using vanilla JS (not jQuery)? Is there a way to do the same thing?
0

Just create variable outside of any functions and it should be available from another js files.

var foo = 'bar'

You may also save variable as a property of window object, which is the root of all objects in DOM, but this approach is not recommended

window.foo = 'bar'

after that, foo var may be accessed in two ways

window.foo

or just

foo

Although I'd recommend to stick with the first approach - general rule is to try to keep your variables as local as possible.

Here is more info about JS variable scopes http://www.w3schools.com/js/js_scope.asp http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/

I'd highly advice you to read this articles(and maybe some more) on the topic - JS scoping is a tricky thing.

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.