1

This is my HTML code:

 <form name="edit" method="post">
        <div><select name="Edi" id ="Edi" >
        <option selected="selected">Select</option>
        <option value="1.php?table= Shift">Shift</option>
        <option value="1.php?table= Start">Start</option>
        <option value="2.php?table= Report">Report</option>
        </select>
    <input onclick="return Edit();" type="submit" value="Edit"/></div>
 </form>

Here is the Javascript code:

function Edit(){
    if(document.forms['edit'].Edi.value == "Select")
        {
            alert("Please Select Edit Field");
            return false;
        }
    else
    {
        window.open(Edi.options[Edi.selectedIndex].value);
        return true;
    }
}

This works, great but it opens the link once Edit button is pressed on a new page. I looked online and for it to do that you have to use "_self". So i changed that line to:

window.open(Edi.options[Edi.selectedIndex].value,"_self");

But when i click the button it just refreshes the current page. Is there something wrong in my code?

I tried this and it doesnt work as well.

window.location.href = Edi.options[Edi.selectedIndex].value;

Is this possible to open the link on the same tab?

USER420
  • 337
  • 3
  • 12
  • possible duplicate http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript – Nikos M. Aug 19 '15 at 18:15
  • I think so because when i am using it without "_self", it open the right page. My only problem is that it is new tab. – USER420 Aug 19 '15 at 18:19
  • Have you checked your browser settings for new windows/redirects? `window.location.href` should work properly – user1102901 Aug 19 '15 at 18:20
  • @Seth `form` does not appear to have value `Select` at any `option` element `if(document.forms['edit'].Edi.value == "Select")` ? _"_"This works, great but it opens the link once Edit button is pressed on a new page."_ Is expected result that current `window.location` be set to selected `option` value ? Also note `" "` space character between "table=" and "Shift" at `1.php?table= Shift` – guest271314 Aug 19 '15 at 18:23
  • Not really helpful to answer the question, but instead of using `Edi.options[Edi.selectedIndex]` you can simply use `Edi.value`, I believe – markasoftware Aug 19 '15 at 18:24
  • @NikosM. I don't think he's trying to do that, he's just trying to redirect, I think – markasoftware Aug 19 '15 at 18:26
  • @user1102901 Now, intresting your bring that up. So window.open (without _self) only works on chrome. It does not work on IE(10). I tried window.location.href it doesnt work on both of them. – USER420 Aug 19 '15 at 18:29
  • `window.location.href` seems to work perfectly fine in this codepen: http://codepen.io/anon/pen/MwMwPM and in case it matters I'm running Chrome 45 beta on Arch Linux. But it should work in any browser, I would think – markasoftware Aug 19 '15 at 18:32
  • @guest271314 I dont know what you mean. Form does have the select option. I am just trying to create a check to make sure user selects something other than SELECT. If select is selected it just gives a alert. – USER420 Aug 19 '15 at 18:32
  • @Seth Which `option` element has value set to "Select" ? – guest271314 Aug 19 '15 at 18:35
  • @Markasoftware i see that. That is what i am looking for. Any idea why its not working on mine(probably some browser setting im thinking)? Using Windows 7 with Netbeans. – USER420 Aug 19 '15 at 18:36
  • does that codepen work for you? If so, it's not browser settings or anything like that – markasoftware Aug 19 '15 at 18:44
  • Well, yeah i went on codepen. If i click edit on select, it gives me the alert. But if i select any other three options and then click edit it says; You just Posted to Pen. But it shows values posted to be 1.php?table= Shift etc. I also changed windows.location.href to windows. open and that opened a new tab. What could it be then? – USER420 Aug 19 '15 at 18:48

0 Answers0