1

This is my js code :

<script type="text/javascript">

var url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.webServerRelativeUrl;
var pngPath = '/_catalogs/masterpage/assets/tools-banner.jpg'; 
var elem = document.createElement("img");
elem.src = url + pngPath;
elem.setAttribute("alt", "Banner");
document.querySelector("#banner").appendChild(elem);

</script>

when i test it in the browser console it work just fine , but when i put it in the of my master page it doesn't work should i load jquery? any solution ?

2
  • You must wait for the DOM to be loaded before executing the code, see here stackoverflow.com/questions/9899372/… Commented Jun 29, 2016 at 9:11
  • where should i add the dom? Commented Jun 29, 2016 at 9:38

1 Answer 1

0

Did you end the script with tag? better copy your code in a text file ( remember not to add tag in the text file, just the code) and save it as .js file. Load the file in Style library and publish it.

Now in Master page add script tag <script type="text/javascript" src="link where javascript is loaded"></script>

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jun 29, 2016 at 15:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.