First: as you use jQuery(document).ready()
your page is already loaded, when the following code is executed.
Second: location.search
contains all of the url past th ?
.
To parse it, see Get escaped URL parameter.
To sum it all:
<%= javascript_tag do %>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
jQuery(document).ready(function () {
$(document).on('click', 'a#user', function() {
$(".box#input").val($(this).attr('value'));
var input = $(".box#input");
$(document).scrollTop(input .offset().top - 45);
input.focus();
});
if (getURLParameter('mode')==1) {
var input = $(".box#input");
$(document).scrollTop(input .offset().top - 45);
input.focus();
}
});
<% end %>
after all your comments I wrote an example page with your code snippet in it and it works:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="includes_js/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
};
jQuery(document).ready(function () {
// alert(location.search);
var mode=getURLParameter('mode');
// alert(mode);
if (mode==1) {
var input = $(".box#input");
$(document).scrollTop(input .offset().top - 45);
input.focus();
}
});
</script>
</head>
<body>
<form>
<input name="t1" /><br>
<input name="t2" class="box" id="input" />
</form>
</body>
</html>
For me, your selector is pretty strange. Do you really have a tag with class="box" id="input"
? Espessially the id seems strange to me.