0

I am using following code. The first if condition is working, but the second if is not. I don't know why? The same CSS should be applied in the second if also. Can anyone tell me why this is?

<script type="text/javascript">
    if (location.pathname.indexOf("girl-clothing") != -1) {
        $(".RetailPriceValue").css("cssText", "margin: -203px 0 0 0 !important;");
    }

    if (location.pathname.indexOf("http://www.xyz.com/girl-clothing/?sort=featured&page=2")!= -1) {
        $(".RetailPriceValue").css("cssText", "margin:-220px 0 0 0 !important;");
    }
</script>
2
  • What is that 'cssText' property name? Commented Dec 12, 2012 at 9:38
  • escape sequence of the indexOf ? Commented Dec 12, 2012 at 9:43

3 Answers 3

1

I would imagine that the first style change is working by coincidence. What you're actually adding is this:

<div style="cssText: margin: -203px 0 0 0 !important;"></div>

The problems here are obvious. Try this instead:

if (location.pathname.indexOf("girl-clothing") != -1) {
    $(".RetailPriceValue").css("margin", "-203px 0 0 0 !important;");
}

if (location.pathname.indexOf("http://www.xyz.com/girl-clothing/?sort=featured&page=2")!= -1) {
    $(".RetailPriceValue").css("margin", "-220px 0 0 0 !important;");
}
Sign up to request clarification or add additional context in comments.

5 Comments

No its not working second css is not applying there.it is taking first css only.why?
Try removing the !important flags. Does the condition definitely hit? If you put an alert() in the second if statement does it appear?
In that case the problem is with your second if statement not matching the url - nothing to do with the .css()
then how to use that url?i want to apply it in that page?
You may want to double check the URL matches then. It's probably best to ask a different question about that though.
1

use something like

$(".RetailPriceValue").css( "margin","-220px 0 0 0 !important");

so try instead

if (location.pathname.indexOf("girl-clothing") != -1) {
    $(".RetailPriceValue").css("margin","-203px 0 0 0 !important");
}

if (location.pathname.indexOf("http://www.xyz.com/girl-clothing/?sort=featured&page=2")!= -1) {
    $(".RetailPriceValue").css( "margin","-220px 0 0 0 !important");
}

3 Comments

I have removed it,but still second css is not applying.atleast can you tell me how to remove "margin: -203px 0 0 0 !important" at this "xyz.com/girl-clothing/?sort=featured&page=2" url???
what do you mean by stray??I think becoz of girl clothing it is taking previous css.isnt it?
@user1326201 is my anser helping or solving your problem ...?
1

if your location.href is http://www.xyz.com/girl-clothing/?sort=featured&page=2

the location.pathname will be "/girl-clothing/", so indexOf function never will find the string "http://"

https://developer.mozilla.org/en-US/docs/DOM/window.location

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.