0

I am developing my own template selector for CSS templates. At the moment, it works fine, clicking on a name from the selectbox changes the template. However there is 1 function that is missing.

If you look here: http://www.demo.joomforest.com/?template=corporate

you can obviously see the URL contains ?template=corporate at the end of it, thus if you enter that URL, it loads the corporate template.

This is my selector so far: http://joomjunk.co.uk/demo

As you can see, I added a hash + template name to the end of the url upon selecting a template, however if you copy and paste that URL into a new tab for example, it loads the default template.

So my question is, how can I add something similar to ?template=corporate and have it load the correct template upon loading that specific URL?

I'm not asking for the full code, but to point me in the right direction.

Thanks in advance.

4
  • Why doesn’t window.location.hash work? Seems like a reasonable way to load templates from URL parameters. Commented Aug 30, 2012 at 16:14
  • location.search perhaps? I'm not entirely sure what you exactly would like to achieve. Commented Aug 30, 2012 at 16:16
  • @David hash from what I have read, hash is an anchor. pimvdb, if you click the link I provided in my question, you will see it loads the Corporate template due to the ?template=corporate on the end of the url. This is what I would like to cheive. Commented Aug 30, 2012 at 16:20
  • @Lodder if you have an URL like http://foo.bar/#corporate you can just check the window.location.hash in your javascript and load that template. Commented Aug 30, 2012 at 16:29

2 Answers 2

1
window.location.search

This will contain whatever comes after the ? in your URL, including the ?

if

window.location.href === "http://www.demo.joomforest.com/?template=corporate"

then

window.location.search === "?template=corporate"

if you want to parse the query string, there are many resources available. If, however, you just want to do it for this specific case:

var tmpl = window.location.search.split('=')[1];
// tmpl === "corporate"

This will split the string on = and then return the second element in the array

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for pointing me in the right direction. I have done it for 1 template, now need to do the rest or find out how to do something along the lines of a foreach command
1

If you have an URL like http://foo.bar/#corporate you can just check the window.location.hash in your javascript and load that template immediately.

Seems reasonable since you already add hashes in your dropdown – the user will see the same template if they choose to refresh the page.

You might even be able to trigger a click event on the right dropdown menu item based on the hash property, but that would require the DOM to be fully loaded.

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.