-3

I need to add a 'Send Message' button which takes the username of the user that message will be sent to, goes to a link and put this username to the 'To' field of the message form.

This is my code

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;
}

Now the problem is that the new page is opened with the parameter. And as a seperate case I can get a parameter from a URL then put it in a field. But I cannot do these two together.

Thanks for the answers!

Mert
  • 1
  • 1
    You're redefining `x`, overwriting its previous value. You have other problems, too, such as your definition for `z`, which doesn't do what your code comment says it does. Considering breaking this down and asking one more specific question. – isherwood Sep 19 '14 at 20:19
  • Get rid of `var` in this line: `var x = x.substring(x.indexOf(":") + 1);` – emerson.marini Sep 19 '14 at 20:20
  • UM, if you change the url, the code after it will not run!!!! The page will exit. The code does not magically run on the next page. That code would have to live on the next page and run onload or document ready! – epascarello Sep 19 '14 at 20:20
  • As soon as you do `window.location.href =`, which is equivalent to `location =` by the way, you change pages. – StackSlave Sep 19 '14 at 20:22

2 Answers2

0

What you are trying to do is impossible.

As I said in my comment you are navigating away from the page when you set the location. The code after that will not run on the next page. That code needs to live on the next page. That code would have to be executed on page load or document ready.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

ok. thanks for the answers. I solve the issue and it works. I am posting the code to help anyone struggling on this problem like me.

The code is divided into two pieces.

<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>
Mert
  • 1