0

I have some HTML code and there somewhere I want to display a video with Javascript. I have a function that gives me a valid link to the video player and source. So if I post it directly without function in src it works fine.

So my question is how can I use my function in the src tag?

<script type="text/javascript" src="get_link()"</script>

A solution without jQuery/document.* is wanted if possible. I know I could do something like that document.getElementById("iframeid").setAttribute("src","link").

Edit

All of the solutions were not working in my case. But anyways it's not a solution for me to do it in Javascript because of security issues. And there is no way to use nodejs. So thanks for your comments anyway. =)

4
  • 1
    Possible duplicate of Dynamically add script tag with src that may include document.write Commented Feb 4, 2016 at 10:36
  • with the same method you need to generate the entire script tag. Commented Feb 4, 2016 at 10:36
  • "A solution without jQuery/**document.*** is wanted if possible." You'll have to explain why you have such an odd requirement. You need to manipulate the DOM somehow. Commented Feb 4, 2016 at 12:09
  • Please add additional details. It sounds like you're trying to do something very strange. Why can't you manipulate the DOM? Commented Feb 4, 2016 at 14:30

2 Answers 2

1

Try adding script in JavaScript like this:

var script = document.createElement('script');
script.setAttribute('src', get_link());
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
Sign up to request clarification or add additional context in comments.

4 Comments

I don't want to have the script in the head. =/
@kwoxer, If you are loading it asynchronously, How does matter where it is placed ?
@kwoxer then append to the body.
It just pastes document.write on my HTML page. Maybe I have some code affecting this? I gonna create a fiddle.
0

If you want to set this attribute through JavaScript, you can't do so by directly putting it in the src attribute. You can attach some event handler on the HTML, such as onclick="handler()", or something of that sort, which when triggered will give you access to the element within the function. Something like this.

function handler() {
    this.src= 'my_link.js
}

EDIT:

Not that the above solution is ideal, but just to meet the criteria of the question. It is possible, without any use of document to do this.

On your script tag, set an onerror="change_link()". Then define an

function change_link() {
    this.src= get_link();
}

And last, you should add a faulty src attribute to the script tag. Again, this is stupid and you shouldn't do it in production code, but I'm just showing you that it's possible.

4 Comments

You mean? <script type="text/javascript" onload="this.src=get_link();"></script> That didn't work either.
That would be the cleanest way, however onload will not trigger unless the script tag already has something to actually load. AND once you set a new src for it the event will trigger again.
It would be awesome if you could write a complete example. Because I also tried <script type="text/javascript"> this.src=get_link(); </script>. This as well did not work. What am I doing wrong?
That's not what I meant. I'll edit my answer to show you a solution to match all your criteria, but you absolutely should not use it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.