views:

441

answers:

5

I'm not sure if I have the jargon for asking this question not being a web developer but please bear with me.

I want to send parameters to a client side HTML page (just a file on a disk no web server involved). My initial attempt was to use a query string and then parse it from window.location.href but instead of the query string being passed to the page I get a file not found error.

Is it possible to do what I'm attempting?

A: 

What about using a frameset and accessing the document object model of the loaded frame?

Josh Einstein
A: 

do you mean you want something like

window.location.search

http://developer.mozilla.org/En/DOM/Window.location

search: the part of the URL that follows the ? symbol, including the ? symbol.

動靜能量
A: 

Do you mean getting the parametere value in a html page? You can use javascript to retrieve the querystring values. Here is an example

Suppose I have a html page with the following link

<a href="param.html?one=8&t=3">HTML Param example</a>

The below code in param.htmle will loop through all the querystring params and display them.

<script language="JavaScript">

var searchStr = location.search;
var section = location.hash.substring(1,location.hash.length);
var searchArray = new Array();

while (searchStr!='') {
    var name, value;
    // strip off leading ? or &
    if ((searchStr.charAt(0)=='?')||(searchStr.charAt(0)=='&')) searchStr = searchStr.substring(1,searchStr.length);
    // find name
    name = searchStr.substring(0,searchStr.indexOf('='));
    // find value
    if (searchStr.indexOf('&')!=-1) value = searchStr.substring(searchStr.indexOf('=')+1,searchStr.indexOf('&'));
    else value = searchStr.substring(searchStr.indexOf('=')+1,searchStr.length);
    // add pair to an associative array
    searchArray[name] = value;
    // cut first pair from string
    if (searchStr.indexOf('&')!=-1) searchStr =  searchStr.substring(searchStr.indexOf('&')+1,searchStr.length);
    else searchStr = '';
    // debug step
    //document.write(name + ': ' + value + ': ' + searchStr + '<br>');
}

// write table of pairs and hash value
document.write('<table align="center">');
document.write('<tr><td><b>name: &nbsp;</b></td><td><b>value:</b></td></tr>');  
for (var name in searchArray) {
    document.write('<tr><td>' + name + ': </td><td>' + searchArray[name] + '</td></tr>'); 
}
if (section!='') document.write('<tr><td>#: </td><td>' + section + '</td></tr>');
document.write('</table>');

</script>

Some more example here.

Shoban
A: 

Firefox and Chrome will let you do this. But IE won't. IE returns file not found like you said.

file:///D:/tmp/test.htm?blah=1

<script language='javascript'>
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
     hash = hashes[i].split('=');
     vars.push(hash[0]);
     vars[hash[0]] = hash[1];
    }
    return vars;
}
alert(getUrlVars());
</script>
Jack B Nimble
+4  A: 

You might want to pass parameters using the # instead of ? on local files.

jensgram