Sohil
Sohil

Reputation: 3

Javascript function working strangely during the first call in CHROME?

HI all,

Below mentioned javascript code works fine in all browsers including chrome(from second call onwards).

function call(val){
        url = window.location.href;
        indexnum = url.lastIndexOf("/");
        str = url.slice(indexnum+1);
        window.location.href = url.replace(str, "sample.php?src_q=") + val;
    }

I am calling this function on onclick of a link as below

<?php  echo "<a href='#' onclick='javascript:call(\"$fieldvalue\");'>$fieldvalue</a>"  ?>

Normal Behaviour : In all browser after clicking on the link new formed url is

url://localhost/mysite/sample.php?src_q=val

Strange Behaviour : When I click on the link for the first time in chrome value of variable val gets replaced by url and its value as follows

http://localhost/mysite/sample.php?src_q=http://localhost/mysite/val

This strange behaviour happens during the first click in chrome. From the second call onwards in the same tab, the value of variable val works fine and I get desired url.

I tried to google on it, but couldn't found any explanation.

Thanks in advance.

Upvotes: 0

Views: 342

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074028

A couple of things:

  1. Drop the "javascript:" from the beginning of the onclick. You'd use that in an href, not in an onclick.

  2. You might want to cancel navigation when the user clicks the link, by returning false:

    onclick='callSearch2(...); return false;'
    

Upvotes: 1

Related Questions