WhiteBr0wnie_24
WhiteBr0wnie_24

Reputation: 149

Javascript throws typeerror on ios only

I have a strange problem and I bet it is just a really stupid error somewhere, but I just can't figure out, what's wrong with my code..

I have a website that works perfectly fine on every (common) browser on every common os. On this page i have a searchbar, which also works perfectly fine on computer. This bar also has a jquery keyup event, to start the search. This event does not fire on ios (first problem). So I added a button as workaround. But this also doesn't work on ios. If it would work correctly, it should call a JS-Method that only redirects the user to the search.php page while appending a GET-Variable called query. I loaded the page on pc and the error console says:

TypeError: is not a function (evaluating 'search()')

It somehow does not seem to load this method. For whatever reason. All other JS-Methods work fine. Just this one refuses to work.

The searchbar:

<input placeholder="Search" id="searchbar">

the link looks like this:

<a href="#" id="searchbutton" onclick="search();">start search</a>

The JS-Method looks like this:

var text = document.getElementById('searchbar').value;
window.location.href="./search.php?q="+text;

Any ideas?

Upvotes: 0

Views: 74

Answers (1)

Alex Lau
Alex Lau

Reputation: 1697

Seems global variable search is reserved by iOS. Try to use some another name like mySearch then it works.

BTW, you may consider using namespace or scope to avoid conflict with libraries or system.

Example:

(function() {
    function search() {
       alert('test');
    }
    document.getElementById('searchbutton').onclick = search;
})();

Or if you insist to use onclick directly in the element.

<a href="#" id="searchbutton" onclick="MyNamespace.search();">start search</a>

<script>
var MyNamespace = (function() {
    function search() {
       alert('test');
    }
    return {
        search: search
    };
})();
</script>

Upvotes: 1

Related Questions