Lodder
Lodder

Reputation: 19733

addition to url

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.

Upvotes: 0

Views: 238

Answers (2)

David Hellsing
David Hellsing

Reputation: 108500

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.

Upvotes: 1

jackwanders
jackwanders

Reputation: 16030

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

Upvotes: 1

Related Questions