3

I'd like to be able to provide URLs to access different views of visualizations that I've created. For example, some of the visualizations change for each year or for each variable. When the user changes the year or the variable depicted, I'd like to update the URL. For example, from

index.html?year=1790&variable=population

to

index.html?year=1820&variable=populationdensity

Then, when a visitor comes to one of those URLs, to initialize the visualization using the query string.

Perhaps I'll have to write this myself, but has anyone done something similar or have sample code for how to do this?

5
  • what technology are you uysing? is this going to be a SPA or a server side page, do you have JS router in place? if you are goign to just rely on JS you can always parse the query string and apply what ever parameters you have to the d3 script. Commented Jun 2, 2014 at 17:34
  • I intent to do this entirely in client-side JavaScript. Commented Jun 2, 2014 at 17:51
  • ok what JS frameworks are you using? Commented Jun 2, 2014 at 18:37
  • Just d3. I just want the users to be able to bookmark/link to more than the initial state of the visualization. Commented Jun 2, 2014 at 21:08
  • There's nothing for this in D3. Commented Jun 2, 2014 at 23:09

1 Answer 1

2

on your script you can do something like

var i=0;
var telem;
var search_values=location.search.replace('\?','').split('&');
var query={}
for(i=0;i<search_values.length;i++){
    telem=search_values[i].split('=');
    query[telem[0]]=telem[1];
}

console.log(query);

after this the query variable should hold all the key:values of your query string. this of should be contained inside a function to avoid polluting the global scope

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

1 Comment

I assume that var tele should be var telem

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.