javascript problem in mozilla obj.click()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rahulgupta

    javascript problem in mozilla obj.click()

    i have a textbox and a save_btn which is a hyperlink. when ever enter
    key is pressed there is a javascript which check if the name in the
    textbox contains any special characters. but when we press enter it do
    page postback and does not check for special characters.

    i have written a javascript function which stops the post back on
    enter press in textbox. and then i raise the onclick event by
    document.getEle mentById(btn_Sa ve).click(). in IE it works fine, but,
    for mozilla and safari it just do the post back on enter key press.


    I also tried to dispatch a event for mozilla but it isn't working.
    Here is my code



    function KeyDownHandler( e , btn_Save)
    {


    var browser=navigat or.appName;
    evt = e || window.event ;
    if (evt.keyCode == 13|| evt.which == 13)
    {
    if(browser == "Microsoft Internet Explorer" )
    {
    evt.returnValue =false;
    evt.cancel = true;
    document.getEle mentById(btn_Sa ve).click();
    }
    else
    {
    evt.preventDefa ult();
    evt.returnValue =false;
    evt.cancel = true;
    var e1 = document.create Event('HTMLEven ts');
    e1.initEvent('c lick', false, false);
    document.getEle mentById(btn_Sa ve).dispatchEve nt(e1);
    }
    }
    }

    and this is a code behind

    txtbox1.Attribu tes.Add("onKeyD own", "javascript:Key DownHandler(eve nt ,
    '"+ btn_Save + "');");


    Please somebody help me in solving this problem.
  • Thomas 'PointedEars' Lahn

    #2
    Re: javascript problem in mozilla obj.click()

    rahulgupta wrote:
    i have a textbox and a save_btn which is a hyperlink.
    Make the latter a submit button and your problems will go away.
    function KeyDownHandler( e , btn_Save)
    ^
    It is not a constructor, so it should not look like one.
    {
    >
    var browser=navigat or.appName;
    evt = e || window.event ;
    if (evt.keyCode == 13|| evt.which == 13)
    {
    if(browser == "Microsoft Internet Explorer" )
    Browser sniffing is an error-prone practice that is unnecessary here, ...
    {
    evt.returnValue =false;
    evt.cancel = true;
    document.getEle mentById(btn_Sa ve).click();
    .... you should feature-test whether your objects have those properties instead.

    <http://PointedEars.de/scripts/test/whatamipp.
    }
    else
    {
    evt.preventDefa ult();
    evt.returnValue =false;
    ^^^^^^^^^^^^^^^ ^^^^^^^
    evt.cancel = true;
    ^^^^^^^^^^^^^^^ ^^^
    Why two branches for that at all?
    var e1 = document.create Event('HTMLEven ts');
    e1.initEvent('c lick', false, false);
    `click' is implemented as a *mouse* event, so it would have to be
    `MouseEvents' and the initEvent() call would be missing several arguments.
    document.getEle mentById(btn_Sa ve).dispatchEve nt(e1);
    }
    }
    }
    >
    and this is a code behind
    >
    txtbox1.Attribu tes.Add("onKeyD own", "javascript:Key DownHandler(eve nt ,
    '"+ btn_Save + "');");
    Wrong event, it is only suitable for printable characters. And since no
    standard object would have an `Add' method, you are using some library; not
    showing it makes it impossible for others to be sure what it does. But you
    really do not need any of this. Quick hack:

    <script type="text/javascript">
    function keyDownHandler( e)
    {
    var c = e.charCode || e.keyCode;
    if (c == 13)
    {
    postback();
    return false;
    }

    return true;
    }

    <form action="..." method="post">
    <textarea
    onkeypress="if (typeof event != 'undefined')
    return keyDownHandler( event)"
    ></textarea>
    <input type="submit" value="Save">
    </form>

    However:

    If you mean `textarea' by "textbox", there is no reason not to use an
    `input' element instead, for you do not seem to want or need multi-line
    input/display.

    If you mean an `input' element by that, you need no scripting at all.


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

    Comment

    • Kiran Makam

      #3
      Re: javascript problem in mozilla obj.click()

      On Sep 10, 7:07 pm, rahulgupta <rahu...@gmail. comwrote:
      i have written a javascript function which stops the post back on
      enter press in textbox.
      If a form contains only a text field, it gets submitted when 'Enter'
      key is pressed. You can prevent this without using javascript by
      adding another textfield. You can use display:none to hide the dummy
      textfield and if you don't give a name it will not get submitted also.

      Code:
      <form ....>
      <input type="text" name="abc" value="actual text field">
      <input type="text" style="display: none">
      </form>
      txtbox1.Attribu tes.Add("onKeyD own", "javascript:Key DownHandler(eve nt ,
      For event handling you can use addEventListene r / attachEvent

      Regards
      Kiran Makam


      Comment

      • dhtml

        #4
        Re: javascript problem in mozilla obj.click()

        rahulgupta wrote:
        i have a textbox and a save_btn which is a hyperlink. when ever enter
        key is pressed there is a javascript which check if the name in the
        textbox contains any special characters. but when we press enter it do
        page postback and does not check for special characters.
        >
        Use onsubmit for the form.

        <form onsubmit="alert ('checking data'); return false;"
        action="http://google.com/">
        <input type="text">
        <input type="submit">
        </form>


        You can use attachEvent/addEventListene r pair or a legacy handler.

        attachEvent - e.returnValue = false; // MSIE Events
        addEventListene r - e.preventDefaul t(); // DOM Event
        onsubmit - return false; // Legacy

        Don't use browser detection. Use feature detection.

        if(form.attachE vent) {
        form.attachEven t('onsubmit', checker);
        } else if(form.addEven tListener) {
        form.addEventLi stener('submit' , checker, false);
        }

        Garrett
        >
        >
        Please somebody help me in solving this problem.

        Comment

        Working...