[ 8 posts ]

Brendan Vogt

  • Username: bioluminescence
  • Joined: Wed Oct 27, 2010 5:57 am
  • Posts: 76
  • Offline
  • Profile

window.location not working in button declaration

Post Posted: Sat Jul 21, 2012 5:43 am
+0-
I am using the latest version of YUI3, 3.6.0 pr4

I'm trying to add an click event to a button. I have the following button definition:

Code:
<button id="CancelButton" type="button">Cancel</button>


Here is my JavaScript to add the click event:

Code:
YUI().use('event', 'button', function (Y) {
     var cancelButton = new Y.Button({
          srcNode: '#CancelButton',
          on: {
               'click': function (e) {
                    window.location = '/';
               }
          }
     })
     .render();
});


When I click on the button it does nothing, it just posts back to the current page. Is my code wrong?

Marco Asbreuk

YUI Contributor

  • Username: itsasbreuk
  • Joined: Mon Nov 16, 2009 5:14 am
  • Posts: 455
  • Location: Netherlands
  • Twitter: itsasbreuk
  • GitHub: ItsAsbreuk
  • Gists: ItsAsbreuk
  • IRC: Marco Asbreuk
  • Offline
  • Profile
Tags:

Re: window.location not working in button declaration

Post Posted: Sun Jul 22, 2012 12:27 am
+0-
Hey Brendan,

Better use Y.config.win instead of window. So it works in all browsers.
Try this code and it should work
Code:
YUI().use('event', 'button', function (Y) {
     var cancelButton = new Y.Button({
          srcNode: '#CancelButton',
          on: {
               'click': function (e) {
                    Y.config.win.location = '/';
               }
          }
     })
     .render();
});


Kind regards,
Marco.

Brendan Vogt

  • Username: bioluminescence
  • Joined: Wed Oct 27, 2010 5:57 am
  • Posts: 76
  • Offline
  • Profile

Re: window.location not working in button declaration

Post Posted: Sun Jul 22, 2012 1:26 am
+0-
Hi,

Thanks for the reply.

I tried your code but the form is still posting to itself. I am using Microsoft's ASP.NET MVC. But this won't make the difference. I took everything off the page just to see if it wasn't maybe another element that was causing this issue. Then I had this:

Code:
<form action="/Application/Create" method="post">

     <button id="SaveButton" type="submit">Save</button>
     <button id="CancelButton" type="button">Cancel</button>

</form>


If apply your code changes and click on cancel then the page submits to itself. If I take the button out of the <form></form> tags and place it outside of these tags then it works 100%. Have you any idea why this is like this?

I just need it to redirect to another page, that's all, not submit itself.

Marco Asbreuk

YUI Contributor

  • Username: itsasbreuk
  • Joined: Mon Nov 16, 2009 5:14 am
  • Posts: 455
  • Location: Netherlands
  • Twitter: itsasbreuk
  • GitHub: ItsAsbreuk
  • Gists: ItsAsbreuk
  • IRC: Marco Asbreuk
  • Offline
  • Profile
Tags:

Re: window.location not working in button declaration

Post Posted: Sun Jul 22, 2012 6:04 am
+0-
Hi Brendan,

Have you validated your html?
That might be a problem. Take a look at this ticket: http://yuilibrary.com/forum/viewtopic.php?p=33884#p33884

Regards,
Marco.

Brendan Vogt

  • Username: bioluminescence
  • Joined: Wed Oct 27, 2010 5:57 am
  • Posts: 76
  • Offline
  • Profile

Re: window.location not working in button declaration

Post Posted: Thu Jul 26, 2012 1:41 am
+0-
Thanks.

I did an HTML validate and it did validate successfully. But I'm still stuck with this issue. I noticed that my buttons weren't in a form tag. Then it worked fine. Now I put them in a form tag and it is handling the cancel button as a submit button. I don't want it to be a submit button, just a plain button. You can try it yourself, I made a plain HTML page with just 2 buttons on it like this:

Code:
<!DOCTYPE html>
<html>
<head>
     <title>Test submit</title>
</head>
<body class="yui3-skin-sam">

     <h1>Test submit</h1>

     <form action="/Administration/Department/Create2" method="post">
          <button id="SaveButton" type="submit">Save</button>
          <button id="CancelButton" type="button">Cancel</button>
     </form>
     <script src="http://yui.yahooapis.com/3.6.0pr4/build/yui/yui-min.js"></script>
     <script>
          YUI().use('button', function (Y) {
               var saveButton = new Y.Button({
                    srcNode: '#SaveButton'
               }).render();

               var cancelButton = new Y.Button({
                    srcNode: '#CancelButton',
                    on: {
                         'click': function (e) {
                              Y.config.win.location = '/Administration/Department/List';
                         }
                    }
               }).render();
          });
     </script>

</body>
</html>


Can you please see if you can create a simple page that redirects from 1 page to another? The buttons must be in form tag.

Brendan Vogt

  • Username: bioluminescence
  • Joined: Wed Oct 27, 2010 5:57 am
  • Posts: 76
  • Offline
  • Profile

Re: window.location not working in button declaration

Post Posted: Wed Aug 01, 2012 7:33 am
+0-
Is there no one that can help me? Am I the only one in the whole wide world with this problem? Can someone please post a working solution to my problem. I've already stripped down the markup to a bare minimum. Is this maybe a YUI bug? Can someone from the YUI team have a look at this and get back to me? Doesn't the on click event work between form tags like the way that I have it?

Brendan Vogt

  • Username: bioluminescence
  • Joined: Wed Oct 27, 2010 5:57 am
  • Posts: 76
  • Offline
  • Profile

Re: window.location not working in button declaration

Post Posted: Wed Aug 01, 2012 10:26 am
+0-
I like this framework, although not so many features as YUI 2, so I don't want to revert to jQuery for my functionality.

Juan Ignacio Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 617
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile

Re: window.location not working in button declaration

Post Posted: Wed Aug 01, 2012 4:02 pm
+0-
Honestly, that last comment... not nice.

The code limits of the forum are driving me nuts.

Answer

Click here to see the revision history on this Gist.
  [ 8 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum