4

i have to update user column in JavaScript. I appended the code in master page and set a time out about 3 second. Now, the function doesn't executed unless user f5 the page (click next page -> function not work -> f5 -> work).

setTimeout(function(){
             var elements = document.getElementsByTagName('a');

            for (var i = 0; i < elements.length; i++) {
                var tmp =elements[i];
                if(tmp.innerHTML.indexOf("my checking string") > -1){
                    tmp.setAttribute("href", "/_layouts/15/xxxx.aspx");
                }
            }
            },3000);

update for window.onload enter image description here

king yau
  • 403
  • 1
  • 5
  • 15

1 Answers1

4

need to see code but im going to assume that you need to wrap your functions to wait for the dom to load. On the first instance the code is executed before its ready. Second time code is in memory hence why it works.

$(function(){

  //all code goes here or the first function that calls others 

});

or the longer way:

$( document ).ready(function() {

  //all code goes here or the first function that calls others 

});

http://api.jquery.com/ready/

EDIT

if you cant use Jquery use javascript

<script>
_spBodyOnLoadFunctionNames('setTimeout');
setTimeout(function(){
             var elements = document.getElementsByTagName('a');

            for (var i = 0; i < elements.length; i++) {
                var tmp =elements[i];
                if(tmp.innerHTML.indexOf("my checking string") > -1){
                    tmp.setAttribute("href", "/_layouts/15/xxxx.aspx");
                }
            }
            },3000);
</script>
Ali Jafer
  • 17,808
  • 1
  • 27
  • 41