views:

32

answers:

3

I have two forms on one page and want to have the input boxes focused based on the URL.

So for example: domain.com/Default.aspx#login and domain.com/Default.aspx#register

and the javascript I have this:

        if (window.location.href = '?action=login')
        {
            window.document.getElementById('<%=txtUserName.ClientID %>').focus();
        }
        else if (window.location.href = '?action=register')
        {
            window.document.getElementById('<%=txtRegEmail.ClientID %>').focus();
        }

EDITED

A: 

You can get the url using window.location.href, and do a simple string test on it to see which focus to set. Either form of the url (anchor or querystring) should work.

Edit: You can learn more about the window.location object here.

Edit 2: Given your added code example, it looks like you're using ASP or ASP.Net. If so, you should probably handle this on the server side, having just one focus() call and setting the element id (the "<%= %>" part) on the server side based on the Request.Querystring object.

tloflin
Might be better to use `window.location.hash`.
LeguRi
can you show an example of this location.href please.
Cameron
location.href is just a simple string that gives the current url. You can also use some other properties of the location object if you want to be more specific, as Richard noted; I added a link with more explanation.
tloflin
added some new code to my question. would that work?
Cameron
Almost, you would want to use location.search instead of location.href in that case, because href gets you the full url, whereas search just gives you the querystring piece. Oh, and be sure to use "==" instead of "=", otherwise you'll be *changing* the url instead of comparing to it!
tloflin
A: 
var url = window.location.href;

if (url.indexOf('?action=login', 0) > -1) {
    window.document.getElementById('<%=txtUserName.ClientID %>').focus();
} else if (url.indexOf('?action=register', 0) > -1) {
    window.document.getElementById('<%=txtRegEmail.ClientID %>').focus();
}
Ivo Sabev
Now we're confusing concepts; if he's doing it with a query string, he should read that query on the server side and only produce one of these two statements. If he uses the hash/anchor/# in the URL he should have a JavaScript `if-else`
LeguRi
the first part works but the elseif doesn't the IDS are defo correct :/
Cameron
@Richard You're right, this seems like it should be handled on the server. Cameron didn't specify at first that he was using ASP.
tloflin
doesnt work with my code either seems to hate the the second form :/
Cameron
@Richard he can do it both ways. He can parse the URL in the JavaScript and make the decision or parse it with ASP (I guess that's what he uses) and write the appropriate JavaScript. My solution is with JavaScript as he asked for such, but I agree he should go for server-side parsing and then output the ID with focus() at the end.
Ivo Sabev
actually i did specify at first i was using ASP :) fixed the problem anyways turns out the JS needed to be at the bottom of the page. Thanks
Cameron
@Cameron <body onload="done()"> and then wrap all your javascript inside function done() {} so you are sure it is started on load.
Ivo Sabev
@Ivo Sabev - You're right, he _can_ do it both ways, but I think one shouldn't interpret the query string with JS. His initial question was about using the anchor `#login` and `#register` which is much more consistent with a JS solution; but once he moved over to query string, the whole thing kind of lost cohesiveness! :)
LeguRi
@Cameron, if your problem is fixed you need to accept an answer. I see you've got a 0% acceptance rate and that will probably affect your ability to get answers in the future.
tloflin
+1  A: 

You either want to do this only on the server side using query strings...

(I code too much PHP and my Asp.NET is a little rust so this code snippet might have syntax/langauage errors...)

<%
    if("login".Equals(Request.QueryString["action"])) {
        %>
        window.document.getElementById('<%=txtUserName.ClientID %>').focus();
        <%
    }
    else if("register".Equals(Request.QueryString["action"])) {
        %>
        window.document.getElementById('<%=txtRegEmail.ClientID %>').focus();
        <%
    }
%>

Or on the client side with javascript and the anchor #login

var url = window.location.hash;

if (url == "#action") {
    window.document.getElementById('<%=txtUserName.ClientID %>').focus();
} else if (url == "#register") {
    window.document.getElementById('<%=txtRegEmail.ClientID %>').focus();
}
LeguRi