4

On Sharepoint Online I have created a code snippet and have this code in it:

<div align="center">
  <input type="text" onKeyDown="if(event.keyCode==13) doSearch();" id="SearchQuery" size="100">
  <br/><br/>
  <button type="button" onclick="javascript:doSearch();">Search</button>
</div>

<script type="text/javascript">
    function doSearch(){
        var theSearchQuery = document.getElementById('SearchQuery');
        var theURLToGoTo =  "https://www.google.com/webhp?#q=" + theSearchQuery.value;
        window.location= theURLToGoTo;
    }
</script>

It works great when you type something in the search box and you click the button with the mouse pointer. As in, it opens the Google screen with the search from the text box in the right place. I.e. it does the Google Search. However, when you enter something in the textbox and press enter instead of running the script, it for some reason opens up the edit function of the Sharepoint page?

UPDATE The code works perfectly if on a blank HTML page. Is it because Sharepoint Online blocks the onKeyDown or something?

  • can you try renaming your function to something other than doSearch, this was an internal function name at one point. – Eric Alexander Oct 5 '16 at 0:45
  • thanks Eric, unfortunately that does the same thing! Update, however, I tried making a blank HTML doc on my desktop and the code works perfectly! so I think it is a Sharepoint Online thing. Does Sharepoint Online block onKeyDown perhaps? – vivster81 Oct 5 '16 at 1:10
2

What's happening is that the OOTB Submit handler for the page is getting in the road.

You can add some script like this to disable the enter key firing the submit action for the page. Fair warning: I've not tested this beyond verification that it fixes your problem, unintended consequences may abound.

<script type="text/javascript">
function stopSumbitOnEnter(evt) { 
var evt = (evt) ? evt : ((event) ? event : null); 
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
if ((evt.keyCode == 13) && (node.type=="text")) {return false;} 
} 

document.onkeypress = stopSumbitOnEnter; 
</script>
<div align="center">
<input type="text" onKeyDown="if(event.keyCode==13) doSearch();" id="SearchQuery" size="100">
<br/><br/>
<button type="button" onclick="javascript:doSearch();">Search</button>
</div>

<script type="text/javascript">
function doSearch(){
    var theSearchQuery = document.getElementById('SearchQuery');
    var theURLToGoTo =  "https://www.google.com/webhp?#q=" + theSearchQuery.value;
    window.location= theURLToGoTo;
}
</script>

Disable script lifted from: http://webcheatsheet.com/javascript/disable_enter_key.php

4

This code is working for me:

$(document).ready(function(){
// check if enter button is pressed and then search button clicked event is called
    $('#txtSearch').keypress(function(e){
      if(e.keyCode==13)
      $('#btnSearch').click();
       return 1;
    });
});
1

Okay, I found a possible solution. Try this code:

<script type="text/javascript">
function noenter() { 
return !(window.event && window.event.keyCode == 13); }
</script>
<div align="center">
<input type="text" onKeyDown="if(event.keyCode==13) doSearch();" id="SearchQuery" size="100">
<br/><br/>
<button type="button" onclick="javascript:doSearch();">Search</button>
</div>

<script type="text/javascript">
function doSearch(){
    var theSearchQuery = document.getElementById('SearchQuery');
    var theURLToGoTo =  "https://www.google.com/webhp?#q=" + theSearchQuery.value;
    window.location= theURLToGoTo;
}
</script>
  • nope, that still doesn't work. Here is a gif of what happens, the first time I use the mouse to click on the submit button, and the script works exactly as it should. But the second time I press the Enter key and it, for some reason goes to the edit page. The source seems to be exactly as above, so nothing has been stripped. – vivster81 Oct 5 '16 at 9:02
1

Jim, you were close enough. You were only missing a return false.

<input type="text" onKeyDown="if(event.keyCode==13) { doSearch(); return false; }" id="SearchQuery" size="100">
<button type="button" onclick="javascript:doSearch();">Cerca con Google</button>
<script type="text/javascript">
function doSearch(){
    var theSearchQuery = document.getElementById('SearchQuery');
    var theURLToGoTo =  "https://www.google.com/webhp?#q=" + theSearchQuery.value;
    window.open(theURLToGoTo);
}
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.