0

I'm hoping to call a line of text by using a simple URL parameter. Say I had an ordered list in javascript and on load of url example.com/?i=14 would get the 14th line in my list and place it where desired.

How can I achieve this?

seb
  • 23
  • 1
  • 1
  • 4
  • 1
    possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – haim770 May 19 '15 at 08:39
  • What have you tried so far? Have you tried making a function that does what you describe and calling that when you click a button? – JasTonAChair May 19 '15 at 08:50

2 Answers2

1

I'm not sure what you mean by "call a line of text," but maybe you could do this:

var url = window.location.href;
var queryPos = url.indexOf('i=');
var param = url.substr(queryPos + 'i='.length);

Now param will contain the value of the parameter and you could use it to fetch whatever.

But since you're trying to access a value from a URL with JavaScript, it might be better to make use of # as explained here: How do I get the value after hash (#) from a URL using jquery (there are non-jquery answers as well)

Community
  • 1
  • 1
Michael Pearson
  • 584
  • 1
  • 4
  • 10
0

Hopefully this is what you need. To place an array element where you need it on document load

<div id="placeHere"></div>

In JS

document.body.onload = function(){
    document.getElementById('placeHere').innerHTML = array[14];
}

jQuery

$(document).ready(function(){
    $('#placeHere').html(array[14]);
})
JasTonAChair
  • 1,948
  • 1
  • 19
  • 31