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. =)

kwoxer
  • 3,734
  • 4
  • 40
  • 70
  • 1
    Possible duplicate of [Dynamically add script tag with src that may include document.write](http://stackoverflow.com/questions/13121948/dynamically-add-script-tag-with-src-that-may-include-document-write) – hsz Feb 04 '16 at 10:36
  • with the same method you need to generate the entire `script` tag. – gurvinder372 Feb 04 '16 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_. – Xan Feb 04 '16 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? – zero298 Feb 04 '16 at 14:30

2 Answers2

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);
jcubic
  • 61,973
  • 54
  • 229
  • 402
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.

GMchris
  • 5,439
  • 4
  • 22
  • 40
  • You mean? `` That didn't work either. – kwoxer Feb 04 '16 at 10:41
  • 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. – GMchris Feb 04 '16 at 10:43
  • It would be awesome if you could write a complete example. Because I also tried ``. This as well did not work. What am I doing wrong? – kwoxer Feb 04 '16 at 10:58
  • 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. – GMchris Feb 04 '16 at 11:04