Html page / JavaScript question

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
hgm
Posts: 28387
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Html page / JavaScript question

Post by hgm »

This is perhaps a very silly question, but I am not realy a web programmer, and know next to nothing about JavaScript.

Someone once made the first draft of this nice JavaScript Chess viewer for me (e.g. http://home.hccnet.nl/h.g.muller/goths.html ). It periodically loads a file with moves from another URL, which is currently hard-coded in the program. (And then performs any new moves that were appended to it since the previous load.)

Is there a way to pass arguments to web pages such that the underlying JavaScript program can access them, like a C program gets passed (argc, argv)? I often see links suffixed by '?' and some other stuff, which seems like it is an argument, like when I am using this forum. Is that just a PHP thing,or does it work in general? And if so, how can the JavaScript access the text of the URL that was used to invoke it? (The 'command line', as it were.)

What I would like is to have a single html page with the viewer, but to be able to tell it which file of moves to load for animating the game. It seems wasteful having to duplicate the viewer page and underlying JavaScript many times, just with a different hard-coded link in it for the file with moves.
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Html page / JavaScript question

Post by Edmund »

the object window.location holds everything url related.

what you want is window.location.hash, which returns the part of the url after the filename so for example an anchor or the parameters.

There are not automatic ways to parse these parameters. Either you build your own parser with standard string functions or you will also find plenty of them on the web. In your case where you just have one parameter, stripping the ? from the beginning of the line should be enough.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Html page / JavaScript question

Post by Sven »

Edmund wrote:the object window.location holds everything url related.

what you want is window.location.hash, which returns the part of the url after the filename so for example an anchor or the parameters.

There are not automatic ways to parse these parameters. Either you build your own parser with standard string functions or you will also find plenty of them on the web. In your case where you just have one parameter, stripping the ? from the beginning of the line should be enough.
Maybe you meant window.location.search, not .hash?

http://www.javascriptkit.com/jsref/location.shtml

window.location.search.substr(1) would return the part after the ?.

@HGM: to your original question about "?" (starting the query part of an URI), it is not restricted to PHP but as far as I know you need any kind of dynamic web page to evaluate the query part, since with static HTML you can't do that.

Sven