LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   embedding javascript in url (https://www.linuxquestions.org/questions/programming-9/embedding-javascript-in-url-432421/)

mohit dhawan 04-06-2006 03:06 PM

embedding javascript in url
 
actully what i need to do is select particular options in a page through url;

ie for eg: if ther is a checkbox,, if i click on a particular link it shud be selected can anyone help me witht the script,.


Thank you.

toreric 04-06-2006 04:02 PM

Your question is impossible to understand, please be clear. Are you trying to design a web page having checkboxes (for what purpose)? If so, how is Javascript involved?

mohit dhawan 04-06-2006 04:13 PM

I mean I need not have to explicityly have to click on the check box, to select it.

better example;

u have a certain multiple choice question on ur webpage and chkboxes to mark the answers.
now i want to select the particular checkbox by entering javascript in the adress bar(ie i write the javascript wher we type the url)
and the appropriate checkbox gets selected.

thst the best I can Explain.

thank you.

toreric 04-06-2006 04:22 PM

Cannot imagine how that would be possible! You should check the box with a mouse click, or use the tab key to move to the check box (if you haven't got a mouse) and then check it using the space bar or so. Is this, by the way, a LinuxQuestions topic?

nickj6282 04-06-2006 04:25 PM

This almost works in FF and works even less well in IE when I tried it (but it does sort of work):

Code:

javascript:document.getElementById('checkbox').checked=true;
try it and see if it works for you.

But if you want to pass variables to a page through a GET request (in the URL) then you're going to need to use some kind of server-side language (like PHP) to output the correct Javascript code to your webpage. Or better yet, just have it write out the checkbox data with the value="checked" option.

-Nick

astorm 04-06-2006 04:28 PM

Code:

<html>
<head>
<script language="Javascript">
function checkIt()
{
  document.getElementById("check").click();
}
</script>
</head>
<body>
<input type="checkbox" name="check" id="check" value="Checkbox">
<input type="button" value="Check it!" onClick=checkIt()>
</body>
</html>

Works on Firefox 1.5.

nickj6282 04-06-2006 11:41 PM

Awesome. Works from the address bar too.

95se 04-07-2006 10:41 AM

Here is a more general solution, then when it loads the page, it will be clicked if the address is like, http://www.somesite.com/page/asdf.html?checkthebox=1

Code:

<script>
window.location.href.match(/(^[^\?]+\?)(.*)$/);

params = RegExp.$2.split(/\&/);
for(var i = 0; i < params.length; i++) {
        var tmp = params[i].split(/\=/);
        params[i] = [tmp[0], tmp[1]];
}

for(var i = 0; i < params.length; i++) {
        if(params[i][0] == "checkthebox" && params[i][1] == "1")
                document.getElementById('checkbox').checked=true;

}
</script>



All times are GMT -5. The time now is 04:27 PM.