javascript - Look for a string in URL using test and location.href -


i'm trying create code show text if word "obyn" in url.

     <p id="p1">hello world!</p>         <script>      function myfunction() {          var str = self.location.href;          var patt = new regexp("obyn");          var res = patt.test(str);          return res;      }       if(myfunction()){          document.getelementbyid("p1").innerhtml = "new text!";       else{          document.getelementbyid("p1").innerhtml = "text 2";       }        </script> 

edit: after making changes suggested below, still doesn't work. shows "hello world!".

you can't execute javascript function php,

the solution here want javascript. (you can append 'this text' or "nope" did in php dom or jquery)

you should add line @ end of function :

return res; //returns true or false

then can test like

 if(myfunction()){     var node = document.createelement("a");//this creates "<a> </a>                      var textnode = document.createtextnode("this text"); //this creates text         node.appendchild(textnode);//then appends text div "<a>this text</a>"         document.getelementbyid("mydiv").appendchild(node); //then can append div "<div id="mydiv> </div>   }  else{      //try same job "nope" understand code gave :)  } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -