0

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

4 Answers 4

3

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

if(window.location.href === 'http://somepage.com')
Sign up to request clarification or add additional context in comments.

Comments

1

= 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>");
   }

2 Comments

I was just about to tell you that you'd made the same error as in the original post - then you corrected it ;)
@StephenOrr Yeah it was copy paste and working on it after.. :)
0

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>"); 

Comments

0
$(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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.