kanala Posted March 10, 2008 Share Posted March 10, 2008 Is it possible to use javascript to retrieve information from the URL? Like you can do with PHP e.g. www.google.com/index.php?id=41Does javascript have that ?id thing?I need it because I want to make a shop using html and javascript only, as my host does not allow php. What I need is e.g. if id = 41, then the page is linked to another javascript page named 41.jsThanks Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2008 Share Posted March 10, 2008 You can access the URL through window.location.href. If you only want the querystring (after the question mark), you can use window.location.search. Link to comment Share on other sites More sharing options...
kanala Posted March 10, 2008 Author Share Posted March 10, 2008 Thanks, so if I wanted to change the src to the window.location.search would I do:<script src="window.location.search + '.js'" type="text/javascript"></script>It needs to end in .js Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2008 Share Posted March 10, 2008 If you wanted to change the src to window.location.search, then yes, that is what you would do. That would only be a valid filename though if you only had one parameter on the querystring and it didn't have a value, e.g. index.html?41. Also, I'm pretty sure that the search property includes the question mark, so you would want to strip that out first.http://www.w3schools.com/htmldom/prop_loc_search.asp Link to comment Share on other sites More sharing options...
Peppe L-G Posted March 10, 2008 Share Posted March 10, 2008 I don't know who made this function, and I have never test it, but i think it will work. <script type="text/javascript">//The functionfunction getFormValues() { var search = window.location.search.substring(1); var pairs = search.split("&"); var ret = new Object(); for(var pair in pairs) { ret[pairs[pair].split("=")[0]] = pairs[pair].split("=")[1]; } return ret;}//Have this to call the function_GET = getFormValues();//_GET is now an array with the values of the querystring, just like in PHP.//Example of using:alert(_GET['name']);</script> Maybe this isn't what you seek, but it may become useful. The code above is from PHPportalen (Sweden) (http://www.phpportalen.net/viewtopic.php?p=400967#400967), if anyone care. Link to comment Share on other sites More sharing options...
kanala Posted March 10, 2008 Author Share Posted March 10, 2008 Thanks a lot, but I found that using html and javascript only is a real pain. So I'm using PHP too. Link to comment Share on other sites More sharing options...
ali_ggl Posted March 13, 2008 Share Posted March 13, 2008 Hi you can do it with these steps. <script language="javascript"> var myURL = document.URL;</script> then in variable myURL you can fine the character ? and onward so you have to split manually ur variables and its values.Last thing on next page you can't get the variable values in JavaScript here you need to use PHP or ASP.ThanksAli. Link to comment Share on other sites More sharing options...
Synook Posted March 14, 2008 Share Posted March 14, 2008 Read the previous discussion, Ali... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.