SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    germany
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java Script above HEad tag

    Hello,
    I was developing asp with javascript. I usually dont give importance in placing the javascripts and usually place above the <head> tag.

    Eg Say when you have a form with some elements and when you submit i put the validation function above the <head> tag and it worked and so i did continued like this.But later i have strange problem, where when you submit the form i get 2 records added instead of one, ie same page reloading twice.
    I put a checkcondition and saw an alert saying record already exists, which indicates that page is reloading again.

    I thought that the error may be due to the Script being placed above the head tag.
    For some asp pages when i place the javascript above the head tag, css got messed up and text and all get funky, and so what i did was just placed the javascript below the body tag, and still it worked.

    just studied some article and all says, to put the javascript either
    within the head tag or within body tag or include as js file if it is purely javascript.

    I need the advise of the expert weather part of the problem which i am facing is due to my wrong placing of javascript functions.

    with regards
    vimal
    Knowledge is all about Sharing

  2. #2
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just about anything can happen when code (any kind) is put in the wrong place .... depends on the browser.. putting that js BEFORE the head creates an INVALID html document!

    The article you read was 100% correct ........ either put the code within script tags that are then within the head of the document OR (better way) put the javascript in an external js file and then put a link to that external file WITHIN the head of the document like this:

    <script type='text/javascript' src='your_js_file.js'></script>

    The external way makes life SO much easier!

  3. #3
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    germany
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, external javascript page with extension js is easier option, but the problem with that is, you cant use asp values in those pages, but in my application i am assigning values from asp to javascript variables and so in that case external javascript page is not in the question.
    I like you to explain "the head creates an INVALID html document!" --> for when i put the javascript function before the <head> tag and even after the </body> tag it works as i have expected.So it was rather puzzling why that works when you put those things there.
    thanks and regards
    vimal
    Knowledge is all about Sharing

  4. #4
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, that I can understand as I do the same thing with Java at times.... We would really have to see some of the code to see what the issues are as far as it not working.. Like my Java, your output to the browser is nothing more than html so at that point it should follow the rules of html and be porperly formed and validated ......

    Your code MAY work with the js before the head or after the body but its not in fact correct looking to a browser and thats where some issues can come up.. Id say that if it isnt working when you put the code in the head there has to be something wrong within that block of js itself

  5. #5
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    germany
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the function i am using
    <script language="javascript">
    function LoadNew(VideoPK,UC,CI)
    {
    if (document.FrmFavOption.CmbFavName.value==0 && document.FrmFavOption.TxtFavName.value=="")
    {
    alert("Favorite Name field is empty")
    return false;
    }
    else if (document.FrmFavOption.CmbFavName.value!=0 && document.FrmFavOption.TxtFavName.value!="" )
    {
    alert("Either Select a Favorite Name from list or type your own if not exists")
    return false;
    }
    else if (document.FrmFavOption.CmbGroupName.value!=0 && document.FrmFavOption.TxtGroupName.value!="" )
    {
    alert("Either Select a Group Name from list or type your own if not exists")
    return false;
    }
    else if (document.FrmFavOption.TxtVidName.value=="" )
    {
    alert("Video Name Cannot be empty")
    return false;
    }
    else
    {
    document.FrmFavOption.action='MyFavorite.asp?VedioPK='+VideoPK+'&mod=add&UC='+UC+'&CI='+CI+'';
    document.FrmFavOption.submit();
    return true;
    }
    }
    with regards
    vimal
    Knowledge is all about Sharing

  6. #6
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, thats a GOOD start BUT .... to be able to reference the form elements were gonna have to see the form also ..... please..

  7. #7
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    germany
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <table border="0" cellspacing="15" cellpadding="0">
    <tr>
    <td><form name="FrmFavOption"action="" method="post" id="form1">
    <table width="600" border="0" cellpadding="0" cellspacing="0" id="stdText">
    <tr>
    <td colspan="5"><h1>Add To Favorite/h1></td>
    </tr>
    <tr>
    <td colspan="5"><div id="separator"></div></td>
    </tr>
    <tr>
    <td colspan="5"><table border="0" cellpadding="0" cellspacing="0" id="stdText">
    <tr>
    <td>Her kan du velge om du vil legge til ny favoritt inn i dine eksisterende favorittlister eller om du &oslash;nsker &aring; opprette og legge til denne i en ny liste </td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td colspan="5"><div id="separator"></div></td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td>Favorite Name:*</td>
    <td>
    <%
    set FavListRs1=Server.CreateObject("ADODB.Recordset")
    FavListRs1.open "select [ListCode],[FavListName] from FavouriteList where [UserCode]=" & UserCode & "",con
    %>
    <select name="CmbFavName" class="formList" onChange="OnChange(<%= VedioPK %>,<%= UserCode %>,<%= ProjCode %>)">
    <option value="0"> &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; </option>
    <%
    Do while NOT FavListRs1.eof
    Response.Write "<option "
    if cint(FavListRs1.fields("ListCode")) = cint(FavNameVal) then
    Response.Write " selected "
    end if
    Response.Write " value ='" & FavListRs1.fields("ListCode") & "'>" & FavListRs1.fields("FavListName") & "</option>"
    FavListRs1.MoveNext
    loop
    set FavListRs1=nothing
    %>
    </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OR
    </td>
    <td><input name="TxtFavName" type="text" class="formTextBk">&nbsp;(Add New)</td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td colspan="5"><div id="separator2"><img src="images/spacer.gif" width="7"></div></td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td>Group Name/td>
    <% if Request.Form("CmbFavName") <> 0 then %>
    <td>
    <%
    set FavGroupRs=Server.CreateObject("ADODB.Recordset")
    FavGroupRs.open "select [GroupCode],[GroupName] from FavGroup where [FavListCode]=" & Request.Form("CmbFavName") & "",con %>
    <select name="CmbGroupName" class="formList">
    <option value="0"> &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</option>
    <%
    Do while NOT FavGroupRs.eof
    Response.Write "<option "
    if cint(FavGroupRs.fields("GroupCode")) = FavGroupVal then
    Response.Write " selected "
    end if
    Response.Write " value ='" & FavGroupRs.fields("GroupCode") & "'>" & FavGroupRs.fields("GroupName") & "</option>"
    FavGroupRs.MoveNext
    loop
    set FavGroupRs=nothing
    %>
    </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OR
    </td>
    <td><input name="TxtGroupName" type="text" class="formTextBk">&nbsp;(Add New)</td>
    <% else%>
    <td>
    <input name="CmbGroupName" type="hidden" value="0">
    <input name="TxtGroupName" type="text" class="formTextBk">
    &nbsp;</td>
    <% end if %>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td colspan="5"><div id="separator2"><img src="images/spacer.gif" width="7"></div></td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td>Video Name:*</td>
    <td><input name="TxtVidName" type="text" class="formTextBk" value="<%= VideoName %>"> &nbsp;or (Name your own )</td>
    <td>Selected Video: <%= VideoName %></td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td colspan="5"><div id="separator2"><img src="images/spacer.gif" width="7"></div></td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td>Description:&nbsp;</td>
    <td><input name="TxtVidDes" type="text" class="formTextBkADD"></td>
    <td>&nbsp; </td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td colspan="5"><div id="separator2"><img src="images/spacer.gif" width="7"></div></td>
    </tr>
    <tr>
    <td colspan="5"><img src="images/spacer.gif" width="7"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input name="AddNEW" type="submit" class="formButton" value=" Add " onClick="return LoadNew(<%= VedioPK %>,<%= UserCode %>,<%= ProjCode %>)"></td>
    <td><input name="SubmitGotolist" type="submit" class="formButton" value="Tilbake til favoritter" onClick="return FromShow()">
    <input name="Add" value="1" type="hidden"> </td>
    </tr>
    </table>
    </form></td>
    </tr>
    </table>
    Knowledge is all about Sharing

  8. #8
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a <input type="submit" onclick="return somefunction();">

    then

    function somefunction() {
    do stuff;
    form.action = "somewhere.asp";
    form.submit();
    return true;
    }

    I suggest the following:
    1. remove the event from your submit button, and put it in the form element as onsubmit, ie:
    <form onsubmit="return somefunction();">

    2. Get rid of form.submit(); from somefunction(). Returning true will cause the form to submit by itself.

    That should work. As it is, I can see why it's submitting twice
    - You click submit button.
    - An event handler is called, which submits the form.
    - The event handler returns TRUE, which causes the form to submit again.

  9. #9
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    germany
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Jim Fraser,
    Please verify if i am correct
    function LoadNew(VideoPK,UC,CI)
    {
    document.FrmFavOption.action='MyFavorite.asp?VedioPK='+VideoPK+'&mod=add&UC='+UC+'&CI='+CI+'';
    return true;
    }
    so my function should look like this where i have to remove the form.submit() line.

    and have to add that onsubmit in form tag.

    yes, your logic is correct, may be that is why it adds twice.

    I can implement your logic for this situation.But assume i have a page where i have one form and have 3 submit button, and in that case how i define my onsubmit event in form.I need that help also for, i have many pages which are designed like that also.

    Thanks for giving some hope in identifying the problem for both jim fraser and Dalton
    with regards
    vimal
    Knowledge is all about Sharing

  10. #10
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also change it to <input type="button" onlcick="return whatev();">, buit if the user has JS disabled, they can't submit the form.

    Ideally, use <input type="submit" onsubmit="return whatev();"> and don't submit the form from the JS.

    So, yes... change onclick to onsubmit and remove submit() from your JS.

  11. #11
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you mean. It should work just fine if you simply remove the form.submit() part from your submit button's onclick function, and return true.

  12. #12
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    germany
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, thanks Mr jim Fraser and Young Twig.Let me apply that and test it and let you know if that works or not.

    with regards
    vimal
    Knowledge is all about Sharing

  13. #13
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Man you guys are QUICK today! I go to dinner and come back and you all beat me to the punch .............. great work though, Ill just sit back and watch

  14. #14
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dc dalton
    Man you guys are QUICK today! I go to dinner and come back and you all beat me to the punch .............. great work though, Ill just sit back and watch
    Haha... Sitepoint is awesome.

  15. #15
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    germany
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hai friends,
    I have tested and got some negative results for you
    1. in dreamweaver i dont find onsubmit event for a submit button.
    2. even if i wrote that manually then that is not working or in other words not submitting
    3. and i have tried to remove the document.formname.submit();
    and no luck with submit procedure for form remains the same.

    Need some help
    with regards
    vimal
    Knowledge is all about Sharing

  16. #16
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vimalpercy
    Hai friends,
    I have tested and got some negative results for you
    1. in dreamweaver i dont find onsubmit event for a submit button.
    2. even if i wrote that manually then that is not working or in other words not submitting
    3. and i have tried to remove the document.formname.submit();
    and no luck with submit procedure for form remains the same.

    Need some help
    with regards
    vimal
    Well thats because there is NO onsubmit event for a submit button ....... it goes in the form tag!

    IE:

    <form method="post" action="some_action" onsubmit="return somefunction();">

    <input type="submit" value=''Submit Form">
    </form>

    The onsubmit event fires automatically when they click the submit button!

    IF The function returns true the form submits if it returns false it doesnt.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •