user3545217
user3545217

Reputation: 303

Adding html via Jquery but page just keeps refreshing

I don't have access to change the html directly on page so I'm using jquery to add some text. I have this code so far:

$(document).ready(function(){
     if(window.location.href='http://somepage.com')
 { 
    $(".rightborder.eq(2)").append("<p>some text</p>");
}

The problem is that the text gets added but the page just keeps refreshing, like its doing a loop which cannot end. Can anyone see why?

Thanks

Dan

Upvotes: 0

Views: 40

Answers (4)

Shaunak D
Shaunak D

Reputation: 20646

$(document).ready(function(){
     if(window.location.href ==='http://somepage.com')
     { 
        $(".rightborder").eq(2).append("<p>some text</p>");
     }
});

Use ===(exactly equal to- equal value and equal type) not =(used to assign values) . Javascript Comparison Operators

And you cannot use .eq(2) in that way.

Upvotes: 0

Deepu Sasidharan
Deepu Sasidharan

Reputation: 5309

your if condition is wrong..You should use the comparision operator instead of assignment operator.

Use this..

if(window.location.href == 'http://somepage.com')

Also correct the below code

$(".rightborder.eq(2)").append("<p>some text</p>");

to this

   $(".rightborder").eq(2).append("<p>some text</p>"); 

Upvotes: 0

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

= is to initialize try == for comparision

And

$(".rightborder").eq(2).append("<p>some text</p>");

instead of

$(".rightborder.eq(2)").append("<p>some text</p>");


   if (window.location.href == 'http://somepage.com') {
        $(".rightborder").eq(2).append("<p>some text</p>");
   }

Upvotes: 1

A. Wolff
A. Wolff

Reputation: 74420

You are assigning new href property, you need to check it instead:

if(window.location.href === 'http://somepage.com')

Upvotes: 3

Related Questions