mobilitydave
mobilitydave

Reputation: 1

Add different text depending on URL with javascript

Hi there I need to do something I thinks a bit complicated.

I need to add different text to a DIV depending on the URL, I know what the URL will be in each case (there are 6), so I need the javascript (jquery) to do this I just done know how to do it myself.

Thanks, Dave.

 <script type = "text/javascript">
 function showDiv() {
 var url = window.location.href;
 if (/(Electric-Mobility)/i.test(url)) {
 document.getElementById("content_hide").style.display="none";
 }
 else {
 document.getElementById("content_hide").style.display="block";
 }
  }
 </script>
 <body onload = "showDiv()" >    

This sort of works but doesnt do what i want and also does after page load.

these are the URL ../brands/Electric-Mobility.html?sort=priceasc ../brands/Pride.html?sort=priceasc ../brands/Medical.html?sort=priceasc ../brands/Princey.html?sort=priceasc ../brands/NHC.html?sort=priceasc ../Roma.html?sort=priceasc

for each Url I need different text in div #content_hide

Hope this is clearer

Thanks New code

    <script type="text/javscript">
    function showDiv() {
    var url = window.location.href;

    if (/(Electric-Mobility)/i.test(url)){

    // i changed your old script using jQuery
    $("#content_hide").css("display","none");
     } else {
   $("#content_hide").css("display","block");
   }


   var myURL = url.split('?');
    var myTexts = ["text1","text2","text3"];
     switch(myURL[0]){
        case "http://www.youngsmobility.co.uk/brands/Electric-Mobility.html":
         $("#content_hide").html(myTexts[0]);
     break;
      case "http://www.youngsmobility.co.uk/brands/Pride.html":
     $("#content_hide").html(myTexts[1]);
      break;
     }
     } 
   </script>

    <body onload = "showDiv()" >

Up date to Code

     function showDiv() {
    var url = window.location.href;
    var myURL = url.split('?');
    var myTexts = ["text1","text2","text3"];
     switch(myURL[0]){
     case "www.youngsmobility.co.uk/brands/Electric-Mobility.html":
     $("#content_hide").html(myTexts[0]);
     break;
     case "www.youngsmobility.co.uk/brands/Pride.html":
     $("#content_hide").html(myTexts[1]);
     break;
    }
     } 
     </script>

   <body onload = "showDiv()"

Upvotes: 0

Views: 1195

Answers (2)

mgraph
mgraph

Reputation: 15338

function showDiv() {
    var url = window.location.href;
    if (/(Electric-Mobility)/i.test(url)){

    // i changed your old script using jQuery
    $("#content_hide").css("display","none");
    } else {
    $("#content_hide").css("display","block");
    }
    // my script start here
    var myURL = url.split('?');
    var myTexts = ["text1","text2","text3"];

    switch(myURL[0]){
     case "url1.html":
         $("#div_id").html(myTexts[0]);
     break;
     case "url2.html":
         $("#div_id").html(myTexts[1]);
     break;
    }
}

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

switch(location.href) {
    case "http://example.com/page1":
        // put text for page1 in div
        break;
    // add more cases as needed
    // optionally add a default case.
}

That should get you going.

Upvotes: 3

Related Questions