0

Current variant looks like that (I tried solution offered here: Can't append <script> element):

var s=document.createElement("script");
s.type="text/javascript";
s.src="js/properties.js";
$("head").append(s);

Previous variant was:

$("head").append($('<script type="text/javascript" src="js/properties.js"></script>'));

And both of them don't work. "properties.js" is also in "js" folder, but if I remove this part of path, it doesn't change anything. I also tried to use ' instead " and check addBlock: I had it installed, but it's disabled on this page. Changing "append" function to "appendChild" also didn't help.

"properties.js" contains just one line:

var PREFIX_URL = "http://localhost:8080/app-rest-1.0.0-SNAPSHOT";

And firstly I declare it in "main.js" to which I, in fact, try to connect this file. Explain, please, what I'm doing wrong.

Community
  • 1
  • 1
KOHTPOJIEP
  • 322
  • 1
  • 4
  • 12
  • So you're loading another script file (`main.js`), from which you're trying to load `properties.js`? Also note that `$("head")` returns an array of all the `` tags. Try replacing `$("head").append(s)` with `$("head")[0].append(s)` – Emanuel Vintilă Feb 27 '16 at 18:55
  • Yes. I want to have opportunity to change url, If I'll use real server, for example. This didn't work. – KOHTPOJIEP Feb 27 '16 at 18:58
  • Possible duplicate of [Can't append – Emanuel Vintilă Feb 27 '16 at 19:11

2 Answers2

0

Add all your <script> tags right before the closing </body> tag, because when the browser encounters a <script> tag it begins downloading it and stops rendering of the page. So by placing them at the bottom of the page you make sure your page is fully loaded before trying to interact with the DOM elements. Also $("head") returns an array of all the <head> tags. You should also enclose your calls in a $(document).ready() function.

<!-- Your html tags here -->
<script type="text/javascript">
    $(document).ready(function(){
    var s=document.createElement("script");
    s.type="text/javascript";
    s.src="js/properties.js";
    $("head")[0].append(s);
    });
</script>
</body>
Emanuel Vintilă
  • 1,911
  • 4
  • 25
  • 35
  • The file doesn't have html-tags. It's just for functions for index.html, which is starting page. Should I place here more code? – KOHTPOJIEP Feb 27 '16 at 19:05
  • Didn't work. It doesn't even mark the "var" keyword, so I think IDE doesn't recognize the code correctly. I just copied your code. And if it'll work, should I place this in every .html file? Properties still contains root URL. – KOHTPOJIEP Feb 27 '16 at 19:12
0

I made JSBin example. You can see in console that last script tag is the one you need. So the your code is correct.

If your IDE don't highlight 'var' - it may be error not in javascript. You can place it in a wrong place for example.

Can you provide link to a gist (or pastie.org or smth) for us to better understand your problem.

P.S. The code $("head")[0].append gives me undefined ( note to previous answer)

Shulyk Volodymyr
  • 767
  • 6
  • 11
  • `console.log($("head"))[0].append` returns `undefined` because append doesn't return anything. But `console.log($("head")[0])` gives the expected output. – Emanuel Vintilă Feb 27 '16 at 19:30
  • http://pastie.org/10740120 here. I don't know what is a key for the solution, so I just load all these files. I brought them to the state before trying solutions offered in this post. – KOHTPOJIEP Feb 27 '16 at 19:37
  • Please look [here](https://jsbin.com/hexinoveki/edit?html,js,console). $('head')[0] returns an element that don't have this method. $(...).append is a function and "return function" means "show code of a function" – Shulyk Volodymyr Feb 27 '16 at 19:38
  • I added your code in html into script tag. And it worked - [link](https://jsbin.com/femezuzexu/edit?html,js,console) – Shulyk Volodymyr Feb 27 '16 at 19:43
  • Worked or just had started? May be I'm looking in the wrong direction, but I don't see the result (the page with appropriate elements, I mean). If I try use this code in my localhost, it loads the page, but without "Models" table and button's style neither. – KOHTPOJIEP Feb 27 '16 at 19:49
  • Maybe I should upload some other files or give more information? – KOHTPOJIEP Feb 27 '16 at 20:03
  • [Look](https://jsbin.com/nojafucoja/1/edit?html,js,console). I pasted a script in head. Click a black arrow in Output tab to open in new browser tab (fullscreen mode). There you can see script tag in head and it's result ("it works" text) in console (f12). So this javascript works. Your problem not in this piece of code. Try add script tag on localhost without path in script. Instead add something inside of a tag. If it works then your problem in path (src attribute) . – Shulyk Volodymyr Feb 27 '16 at 20:36