TinyNav – browser resize friendly

I use the TinyNav jQuery plugin to transfer a list menu into a dropdown for mobile devices.

I notice when I resize my browser on my desktop, the TinyNav leaves kind of a nasty look. For example, after I resized my browser into mobile size like 450px wide, I have to refresh the page for the tinynav to take effect and then when I resize my browser back to the normal size, I may see the leftover TinyNav dropdown like:
tinynav

To make tinynav more resize friendly, I implemented the following concept.

To detect mobile, I check if a certain layer is no longer display none. This way I let the CSS media query take full control of when it’s considered mobile. I don’t have to do any javascript detection. So it could be considered mobile just when you resize your browser window on your desktop. This is the concept I’m using; you may not like it but that’s ok.

My implementation also caches the tinyNav plugin so it doesn’t create a new dropdown every time you resize the window.

Here’s an example of the implementation.

  • Load the page
  • Resize your window to be less than 450px wide
  • You’d see the dropdown menu
  • Resize your window slowly to normal size, the dropdown will disappear and the normal menu will show
  • Repeat the steps
  • View source to see the implementation

Continue reading

jQuery mobile 1.2 alpha release popup to work within a page

Holy crap, I thought I was going to die. I couldn’t figure out for the life of me how to get the new jQuery mobile popup to work within a jQuery mobile page. The documentation just shows the sample code outside of a page. Outside of a page it works just fine. But as soon as I put the link inside of a page, nothing works. I tried all sort of ways.

E.g. I tried this form from their example

<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all photopopup">
<div style="padding:10px 20px;">
	  <h3>Please sign in</h3>
  <label for="un" class="ui-hidden-accessible">Username:</label>
  <input type="text" name="user" id="un" value="" placeholder="username" data-theme="a" />

  <label for="pw" class="ui-hidden-accessible">Password:</label>
  <input type="password" name="pass" id="pw" value="" placeholder="password" data-theme="a" />

  <button type="submit" data-theme="b">Sign in</button>
</div>
</div>

When I just used

<a data-rel="popup" id="popupClick" href="#popupLogin">Open Popup</a>

It worked fine but as soon as I put that Open Popup link inside of a div with data-role=”page”, I get no error but when I clicked on the link, nothing happens.

<div data-role="page">
<a data-rel="popup" id="popupClick" href="#popupLogin">Open Popup</a>
</div>

I searched all over the internet and I didn’t really find any exact complaints so I kept thinking I must be missing something simple. But this something simple was eluding me until a century later, I finally smacked my forehead. I just needed to put my popup div inside of the page div like:

<div data-role="page" id="home">
	<a data-rel="popup" id="popupClick" href="#popupLogin">Open Popup</a>
		
		
	<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all photopopup">
		<div style="padding:10px 20px;">
		  <h3>Please sign in</h3>
	  <label for="un" class="ui-hidden-accessible">Username:</label>
	  <input type="text" name="user" id="un" value="" placeholder="username" data-theme="a" />

	  <label for="pw" class="ui-hidden-accessible">Password:</label>
	  <input type="password" name="pass" id="pw" value="" placeholder="password" data-theme="a" />

	  <button type="submit" data-theme="b">Sign in</button>
		</div>
	</div>
</div>

I suppose it does make sense. But there was absolutely no helpful error messages to help me think the jQuery mobile way:
DIV = PAGE!!!

But I’m glad I finally figured it out. Very excited about the new popup widget in jQuery mobile.

My example -> http://regretless.com/stuff/jQueryMobile/jQueryMobilePopup.html (booo it doesn’t work right on my nexus 7. comment submitted to jQuery mobile group. WOW they fixed it!!)

While I was messing around, I learned this very useful jQuery mobile trick though. To bind onclick events in jQuery mobile, you may do:

$("#selector").click(function (e) {
    e.stopImmediatePropagation();
    e.preventDefault();
    //Do important stuff....
    console.log('clicked');
});

jQuery ON (syntax difference from live)

Since jQuery live is marked as deprecated. I thought it’d be a good thing to switch to use jQuery on.

I was at first shocked to find out that “ON” did not work like “LIVE”. Without digging very far, I simply switched my old syntax that had

$('a.delete').live('click', function() {});

to

$('a.delete').on('click', function() {});

It worked for non dynamically generated DOM tree but failed to work on anything that’s dynamically generated. Thank you to stackoverflow, I was able to use the correct syntax for any bubbling event binding to

$(document).on('click', 'a.delete', function(event) {});

Here’s my jsFiddle example to better demonstrate what I’m talking about -> jQuery ON syntax vs. LIVE syntax.