我需要添加一条发送消息'获取将要发送消息的用户的用户名的按钮,转到链接并将此用户名放入' To'消息表格的字段。
这是我的代码
function sendmessage() {
var x;
//geting the username
x = document.getElementsByClassName('sol-kkutu')[0].innerHTML;
var x = x.substring(x.indexOf(":") + 1);
//passing the username parameter by URL
window.location.href = "http://ogrencidenilan.net/mesajlar-2?fepaction=newmessage"+"&" + x;
//to get the username parameter from URL on new page
var z=window.location.search.substr(1);
//to get rid of some part of the username parameter
var a = z.substring(z.indexOf("0") + 1);
a = a.substring(0, a.length - 8)
//to put the username to 'To' field
document.getElementById("search-q").value= a;
}
现在问题是用参数打开了新页面。作为一个单独的案例,我可以从URL获取参数,然后将其放在一个字段中。但我不能一起做这两个。
感谢您的回答!
答案 0 :(得分:0)
你要做的事是不可能的。
正如我在评论中所说,当您设置位置时,您正在离开页面。之后的代码将不会在下一页上运行。该代码需要存在于下一页。该代码必须在页面加载或文档就绪时执行。
答案 1 :(得分:0)
确定。谢谢你的回答。我解决了这个问题并且有效。我发布的代码是为了帮助像我一样在这个问题上挣扎。
代码分为两部分。
<script>
function sendmessage() {
var y;
//geting the username
y = document.getElementsByClassName('sol-kkutu')[0].innerHTML;
var x = y.substring(y.indexOf(":") + 1);
//passing the username parameter by URL
window.location.href = "http://ogrencidenilan.net/mesajlar-2?fepaction=newmessage"+"&" + x;
}
</script>
<script>
//code running after page-load
window.onload = function() {
var z=window.location.search.substr(1);
var a = z.substring(z.indexOf("0") + 1);
a = a.substring(0, a.length - 8)
document.getElementById("search-q").value= a;
}
</script>