0

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()"/>
3
  • Remove return from the onclick and it should work.
    – user2536
    Aug 23, 2012 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 Jafer
    Aug 23, 2012 at 8:39
  • Does it work even if you add it to a content editor webpart? Aug 23, 2012 at 11:53

3 Answers 3

1

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

2
  • but that shouldnt be the problem as iv tested it and it works fine!
    – Ali Jafer
    Aug 23, 2012 at 9:30
  • Thanks man, this problem almost brought me to a mental breakdown. Aug 23, 2012 at 12:04
1

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 causing it to act like it is so you put an alert before 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.

4
  • +1 That was the next step I was going to recommend.
    – Stu Pegg
    Aug 23, 2012 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 Aug 23, 2012 at 11:57
  • Ok how are you adding in the Javascript? are you doing it directly?
    – Ali Jafer
    Aug 23, 2012 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 Aug 23, 2012 at 13:06
0

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.

3
  • 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? Aug 23, 2012 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?
    – Stu Pegg
    Aug 23, 2012 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? Aug 23, 2012 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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