0

I have the following script declared in the document of my head:

<script src="http://www.domain.com/js/widgets.js" type="text/javascript"></script>

Which points to this:

widgets.js

(function () {

    var styleEl = document.createElement("link");
    styleEl.type = "text/css";
    styleEl.href = "http://127.0.0.1:8002/static/css/widgets.css";
    styleEl.rel = "stylesheet";

    //document.getElementsByTagName('head')[0].appendChild(styleEl);
    document.head.appendChild(styleEl);

    document.write("<div id='share_box'>" +
        "<a href='test' target='_blank'>R</a>" +
        "</div>");
})();

I would like to pass in variable to this external file, this is what I have tried:

<script src="widgets.js" type="text/javascript">
     var url = 'https://www.domain.com/link/'
</script>

How can I pass in the variable URL to my external script? (in a safe way using javascript only - no jquery)

4
  • jquery is written is javascript so when you say javascript only it makes no sense. You are mixing the DOM and javascript.Furthermore your question makes no sense at all.You say you want to pass form parameters,but where is the form? Commented Mar 31, 2014 at 15:24
  • sorry, let me explain. I want to pass a variable called url to the external js file. the variable is on the page. Commented Mar 31, 2014 at 15:49
  • 1
    The usual thing would be to declare the variable in a <script> tag above, and then access it. Although if you are going to have more than one you should consider using an object to avoid polluting the global namespace. Commented Mar 31, 2014 at 15:53
  • You would probably also need to notify the widget.js script that a change has been made to url somehow. Simply changing the string value of that variable won't automatically update any objects that use it. Commented Mar 31, 2014 at 16:03

1 Answer 1

1

The links you're providing are both imaginary. There is nothing present there, however the code that you're having is as:

(function () {

    var styleEl = document.createElement("link");
    styleEl.type = "text/css";
    styleEl.href = "http://127.0.0.1:8002/static/css/widgets.css";
    styleEl.rel = "stylesheet";

    //document.getElementsByTagName('head')[0].appendChild(styleEl);
    document.head.appendChild(styleEl);

    document.write("<div id='share_box'>" +
        "<a href='test' target='_blank'>R</a>" +
        "</div>");
})();

Unable to pass a variable (In this example)

Actually, you cannot pass a parameter to it. Because it doesn't allow any.

How a function recieves a parameter

To recieve a parameter, the function must allow a parameter while declaration. Such as:

function someFuncName (params) {
  /* then you can say that the parameters would be sent! */
}

The code you're saying, won't allow any parameters because there isn't any space or permission for a param.

How to pass a parameter

However, to pass on a param, you use the specific term

<script>
  functionName(params);
</script>

Simple it is. But in your case, you cannot!

What the above code does:

What the above code actually does, is that is adds a link element. Such as

<link type="text/css" href="http://127.0.0.1:8002/static/css/widgets.css" 
rel="stylesheet" />

And the next thing it does, is that it would write an element clause of

<div id="share_box">
  <a href="test" target="_blank">R</a>
</div>

Maybe the above code, would create a hyperlink and then style it using the .css file provided at the site that is inside the href.

But the answer is still, that you cannot pass the parameter to an external file until or unless it allows you to.

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

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.