Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

my issue is, that the following script with its window.location function doesn't work in a sharepoint content editor webpart. I've tested it as a standalone and it worked fine, but as soon I inserted it in the content editor webpart nothing seemed to happen when the script was executed.

I would appreciate any kind of help

<script type="text/javascript">
    function redirect(){
       var search = document.getElementById('searchtxt').value;
       var url = "test.com/test.aspx?s="+search;
       window.location = "http://"+url+"";
       return;
    }
    </script>
    <input type="text" id="searchtxt"/> <input type="submit" value="Search" onclick="return redirect()"/>
share|improve this question
    
Remove return from the onclick and it should work. –  Daniel Ziga Aug 23 '12 at 7:22
    
Strange Iv just tested you code without changing it and it works just fine! like stuart said I could be the function name thats screwing it up as it works perfectly for me :) –  ali Sharepoint Aug 23 '12 at 8:39
    
Does it work even if you add it to a content editor webpart? –  Charly Helton Aug 23 '12 at 11:53
add comment

3 Answers 3

up vote 1 down vote accepted

There is a problem in function call you should change

<input type="text" id="searchtxt"/> <input type="submit" value="Search" onclick="javascript:redirect();return false;"/>

issue in onclick="javascript:redirect();return false;"

share|improve this answer
    
but that shouldnt be the problem as iv tested it and it works fine! –  ali Sharepoint Aug 23 '12 at 9:30
    
Thanks man, this problem almost brought me to a mental breakdown. –  Charly Helton Aug 23 '12 at 12:04
add comment

First of all you need to rule out that its firing you function!

add an alert("fired"); just to see that its executing the function. The next step is to see that it is the window.location that is cusing it to act like it is so you put an alert befor and after it like so:

function redirect(){ 
   alert("fired");
   var search = document.getElementById('searchtxt').value; 
   var url = "test.com/test.aspx?s="+search; 
   alert("the url is: " + url );
   window.location = "http://"+url+""; 
   alert("the full location is: " + window.location);
   return; 
} 

Now you should see three message boxes, the first to check to see the function is being triggered, the second to see that the correct url is being added and that it got that far and the third is to make sure the final location url is correct and its gone past that part also.

share|improve this answer
    
+1 That was the next step I was going to recommend. –  Stuart Pegg Aug 23 '12 at 9:36
    
Thanks for your reply. The output is the following: 1. alert: "firing" 2. alert: "test.com/test.aspx?s=asdf 3. alert: "test.com/test.aspx" (old link->not replaced) Somehow it is working in FF now, but it still isn't working in IE8 –  Charly Helton Aug 23 '12 at 11:57
    
Ok how are you adding in the Javascript? are you doing it directly? –  ali Sharepoint Aug 23 '12 at 12:22
    
No, I'm adding it in a content editor webpart which is placed on the page, but V_B just got me the solution, thanks anyway for your time and your help –  Charly Helton Aug 23 '12 at 13:06
add comment

In addition to @Dandroid's comment: You could be conflicting with a built-in SharePoint function name, or a 3rd party JavaScript library.

Try renaming the function to something more unique (e.g. charly_redirect()).

We prefix all function names with an abbreviation of our company name, to avoid these very problems.

share|improve this answer
    
Thank you for your answers. I've tried to remove the return and to rename the function but it still doesn't work. How could I find out if there are some conflicts with the built-in functions? –  Charly Helton Aug 23 '12 at 8:00
    
Renaming should have solved that problem. If you change the onclick to something like window.location = 'http://www.google.com'; does it work? –  Stuart Pegg Aug 23 '12 at 8:07
    
Even this attempt doesn't work. It seems that the editor webpart has some problems with the window.location. Could that be possible? –  Charly Helton Aug 23 '12 at 8:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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