0

I have got a problem with JavaScript. I want to add a drop down list control with a link as Value of it. After it will be choosen and clicked button it schould redirect me to chosen link. I tried this code from this Article:
https://stackoverflow.com/questions/19843373/linking-to-other-pages-in-html-via-drop-down-menu

    <select name="list" id="list" accesskey="target">
    <option value='none' selected>Choose a theme</option>
    <option value="index.html">Theme 1</option>
    <option value="theme2.html">Theme 2</option>
    <option value="theme3.html">Theme 3</option>
<select>
<input type=button value="Go" onclick="goToNewPage()" />

<script type="text/javascript">
function goToNewPage()
{
    var url = document.getElementById('list').value;
    if(url != 'none') {
        window.location = url;
    }
}

but when I tried to add by the script, or by html on site, or by designer its erase me that sort of text:

<input type=button value="Go" onclick="goToNewPage()" />

It's live only:

<input type=button value="Go" />
3
  • I hope you are closing the </script> tag ? Sep 12, 2014 at 11:18
  • Yes I Use this...
    – Grzegorz Z
    Sep 12, 2014 at 11:43
  • Putting in a form or a page?
    – Renanfdo
    Sep 12, 2014 at 11:48

2 Answers 2

1

i know why this doesnt work, as the link states to put it in the head section! but i presume that you dont have a head section so you would add it to the masterpage! either referencing the JS file or inline. Inline means adding it directly to the masterpage.

you never stated how your creating the page! is it a webpart or just a single aspx page with no codebehind? if it contains .cs than you can use this:

http://msdn.microsoft.com/en-us/library/vstudio/ms178207(v=vs.100).aspx

or

front end (aspx)

<input type=button value="Go" onclick="redirect" />

for the codebehind (.cs)

protected void redirect(object sender, EventArgs e)
{
    RegisterStartupScript(this, this.GetType(), "myRedirectScript", "goToNewPage();", true);
}

js file reference within masterpage:

this is within the head tag.

<script src="/_layouts/js/myRedirectPage.js" type="text/javascript"></script>

or add it directly to the masterpage

this is added directly within the head tag.

<script type="text/javascript">
function goToNewPage()
{
    var url = document.getElementById('list').value;
    if(url != 'none') {
        window.location = url;
    }
}
</script>

but this will load the JS script or file to every page unless you use a seperate masterpage

option 1

is fine but your injecting JS directly to the page, this also means its not adding JS reference to every page

option 2

this is adding to every page but you can create a copy of the masterpage just for that location to use that JS file, bit overkill as code wouldnt be called.

option 3

same as option 2

option 2 is best it does allow you to ammend the JS file without checkout of the masterpage to ammend all the time so less prone to break. Its also easy to maintain as a reference.

0

Try adding the code in content editor web part.

3
  • I tried but it not help me I tried By Content Editor, I tried by script Editor, And I tried by Designer, and nothing helps :(
    – Grzegorz Z
    Sep 12, 2014 at 11:08
  • 1
    There must be something wrong in the code. Rearrange the code and check . Ideally it works. Sep 12, 2014 at 11:10
  • I tried 3-4 scripts.
    – Grzegorz Z
    Sep 12, 2014 at 11:17

Your Answer

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

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