0

I need to create a script thant changes the css of a page, depending on a url inserted by the user.

For example

If I use this url: http://mypage.com/ It shows this css: <link rel="stylesheet" type="text/css" href="cssfolder/style.css">

But I need if I use this url for example: http://mypage.com/#color1 (or another url format you consider is better to achieve the same)

I need to change it for the new css, based on the url, and overwrite it, showing this: <link rel="stylesheet" type="text/css" href="cssfolder/user1.css">

I need it uses the css based on the url, replacing the css file name.

This way if a user enters the site http://mypage.com/#color1 it will show the blue.css (changing everything to blue, change logo to blue...) and if enters http://mypage.com/#red, css changes to red.css, showing everything red, but the content is exactly the same.

Anyone has any idea how this can be done?

1
  • 2
    yes it is. Have you tried it? Commented Jun 27, 2014 at 19:28

1 Answer 1

3

This will get your hash value you and load it. If you are changing the # at runtime, you will need to make sure you delete the previous, but this is assuming you will just do this once.

var hash_var = window.location.hash;
if (hash_var){
    $("<link />").attr({
      rel:"styleheet", 
      type:"text/css",
      href:hash_var.substring(1) + ".css"
    }).appendTo("head");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for replying Fallenreaper! I added this to the first part of my code, but it's not changing the css, what can I have done wrong?
What browser are you using? ALSO: check the javascript console, the Sources, to see if it is added to the sources?
ALSO: you need to make sure that it is IN the folder of the file. You know? Some people store css files in a css folder. You would want to make sure that the dir structure is correct
i posted 2 comments. Read them and let me know what you see

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.