preventdefault

href: # or Javascript:void(0); ?

While I’ve been using <a href="Javascript:void(0);"></a> for most of my JS developer days, I really never undestood why this is cosidered better than <a href="#"></a>. Today, on a whim I set out to find out.

Which one is better?

The answer is niether. None of the above options are what you could call “best practice”.

Why? beacause unobtrusive javascript and progressive enhancement are important! If you rely on a hash symbol (it’s actually called a hypertext fragment identifier) your called function must always end in return false;. Because:

…. the final return false; will not execute if the called function throws an error. Hence the developers have to also remember to handle any error appropriately in the called function.

And if you use the <a href="Javascript:void(0);"></a> value your code is not compatible with non-javascript browsers.

So learn to love the event.preventDefault() method and remember to put it in your event function.

// <body onload="initElement()">

function initElement() {
  var p = document.getElementById("foo");
  p.onclick = showAlert(event);
}

function showAlert(ev) {
   ev.preventDefault();
    alert("onclick Event detected!");
}
Using jQuery's preventDefault with CoffeeScript

So you’ve only just started using CoffeeScript, and you need to bind some response to a click event on a link. But you don’t want the user to navigate to that link, you want to, like you alway do, prevent the default behavior. So all you have to do is call preventDefault() on the event object. Right. But how do you get that using Coffee huh? Well here’s how:

some_object.click (e) =>
    e.preventDefault()

event.preventDefault() VS. return false

preventDefault() and return false both do the similar things; they prevent the browser from performing its default functionality for that particular event*

Example

$('a').click(function(e){
  alert('No refresh!');
  return false;
});


$('a').click(function(e){
  e.preventDefault();
  alert('No refresh!');
});


So why use one over the other?

Example 2

$('a').click(function(e){
  alert(nonExistentVar);
  return false;
});


$('a').click(function(e){
  e.preventDefault();
  alert(nonExistentVar);
});

In this example both functions will throw an error. In the preventDefault version you will get to see this error in the console and the page won’t refresh. In the return false example the page will refresh and you won’t have clue what went wrong with your code.

This is because e.preventDefault(); allows you to declare this at the start of the function, whereas return false always has to be declared at the end of the function (once return is called no more code will run). So in the return false example the code errors, never hits the return false command and the browser deals the click how it normally does.

I have encountered this problem before but only now have I realised that e.preventDefault can make my debugging chores that tiny bit easier :)




* return false; will also prevent event propagation/bubbling when used inside a jQuery event handler function. An alternative to this is stopPropagation(). Credit to @murger for spotting this. See this discussion for more details.

Solution: event.preventDefault() function not working in IE. #programming #development #it

Solution: event.preventDefault() function not working in IE. #programming #development #it

event.preventDefault() function not working in IE.

Following is my javascript (mootools) code:

$('orderNowForm').addEvent('submit', function (event) { event.preventDefault(); allFilled = false; $$(".required").each(function (inp) { if (inp.getValue() != '') { allFilled = true; } }); if (!allFilled) { $$(".errormsg").setStyle('display', ''); return; } else { $$('.defaultText').each(function…

View On WordPress

How to: event.preventDefault() function not working in IE.

How to: event.preventDefault() function not working in IE.

event.preventDefault() function not working in IE.

Following is my javascript (mootools) code:

$('orderNowForm').addEvent('submit', function (event) { event.preventDefault(); allFilled = false; $$(".required").each(function (inp) { if (inp.getValue() != '') { allFilled = true; } }); if (!allFilled) { $$(".errormsg").setStyle('display', ''); return; } else { $$('.defaultText').each(function…

View On WordPress

Parem as prensas

Parem as prensas #javascript #jquery #stopPropagation #preventDefault

Esses dias eu fiquei curioso para saber a diferença entre stopPropagation e preventDefault. Segue a explicação que eu achei.

  • stopPropagation é utilizado para ter certeza que o evento não vai “borbulhar” para cima.
    ex: o clique em um também ir gerar um click no <td> dispara um clique no <tr> e no <table>. O stopPropagation evita que isso ocorra.
  • preventDefault é utilizado para evitar a ação normal…

View On WordPress

The Ultimate PreventDefault Function
function preventDefault(e) {
	// Trying all the various prevent-Default mechanisms
	if (e.preventDefault) {
		e.preventDefault();
	}
	if (e.stopImmediatePropagation) {
		e.stopImmediatePropagation();
	}
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	if (e.stop) {
		e.stop();
	}
	e.returnValue = false;
	return false;
}
  • This is standard function that can be used to prevent default functions to perform other tasks.
  • This function would make sure only the current event handler works and all the others keep quiet.
  • All the possible functions e.preventDefault(), e.stopImmediatePropagation(), e.stopPropagation(), e.stop() are used to avoid cross browser compatibility issues that make return false buggy.


  1. event.preventDefault() => Cancels the event if it is cancelable, without stopping further propagation of the event.
  2. event.stopPropagation() => Stop further propagation of the event through the DOM.
  3. event.stopImmediatePropagation() => When several listeners are attached to the same element for the same event type, they are called in order in which they have been added.So this function would stop all other listeners from being called.
  4. event.stop() =>Stops the event from being sent to further elements.
  5. e.returnValue =>Works as return false.
  6. return false => event.preventDefault() + event.stopPropagation()
Resolved: What's the difference between event.stopPropagation and event.preventDefault? #dev #computers #fix

Resolved: What’s the difference between event.stopPropagation and event.preventDefault? #dev #computers #fix

What’s the difference between event.stopPropagation and event.preventDefault?

They seem to be doing the same thing… Is one modern and one old? Or are they supported by different browsers?

When I handle events myself (without framework) I just always check for both and execute both if present. (I also return false, but I have the feeling that doesn’t work with events attached with node.addEventLi…

View On WordPress

return false, preventDefault, and stopPropagation in jQuery

During my talk at the second Manchester jQuery meetup, I used the .preventDefault() markup in my click event handler.

$('a').click( function(event) {
  event.preventDefault();
});

Upon inserting this, I was asked what the difference was between using this method and using return false. I knew I had read about this particular use case before, and clearly some of it had stuck as I was still using .preventDefault() but my answer pretty much boiled down the fact that this is my prefered method.

My reasoning was that it made more sense to me on reviewing my code, when I see return false for example, I am automatically assuming that I am trying to escape out of a loop or returning the value false.

Whereas if I see .preventDefault() I assume that I am trying to prevent the default action. However, as I’d been asked the question, I decided to dig a little deeper.

In jQuery, if a developer stops the default behaviour by using return false they are actually getting a little bit more than they bargained for.

Not only does this call have to take place right at the end of the resulting function they are also running both the .preventDefault() and the .stopPropagation() javascript calls.

$('a').click( function(event) {
  return false;
});

is equivalent to

$('a').click( function(event) {
  event.preventDefault();
  event.stopPropagation();
});

To understand what this means, let’s look at the W3C definition for these calls:

.preventDefault()

If an event is cancelable, the preventDefault method is used to signify that the event is to be canceled, meaning any default action normally taken by the implementation as a result of the event will not occur. If, during any stage of event flow, the preventDefault method is called the event is canceled. Any default action associated with the event will not occur. Calling this method for a non-cancelable event has no effect. Once preventDefault has been called it will remain in effect throughout the remainder of the event’s propagation. This method may be used during any stage of event flow.

.stopPropagation()

The stopPropagation method is used prevent further propagation of an event during event flow. If this method is called by any EventListener the event will cease propagating through the tree. The event will complete dispatch to all listeners on the current EventTarget before event flow stops. This method may be used during any stage of event flow.

Source: http://goo.gl/VBpEp

So, if we only want to stop the default action occuring on our element we should use .preventDefault(), if we want this to stop from bubbling up the DOM then we can use .preventDefault() along with .stopPropagation() or we can use return false. If you want your click event to fire on the elements parents then just use .preventDefault(). Using return false means that the event will not fire on the element or any of its parents. A brief example can be seen here here and a more in depth overview can be seen here.

How to: What's the difference between event.stopPropagation and event.preventDefault?

What’s the difference between event.stopPropagation and event.preventDefault?

They seem to be doing the same thing… Is one modern and one old? Or are they supported by different browsers?

When I handle events myself (without framework) I just always check for both and execute both if present. (I also return false, but I have the feeling that doesn’t work with events attached with node.addEventLi…

View On WordPress

New! Chapter 2015 Spring/Summer Collection | Streetvoyage.com

American Fashion Brand Chapter now presents us with their new 2015 Spring/Summer collection.

加州时装品牌 Chapter 日前发布了品牌最新的春夏系列 lookbook 。

大家可能对此品牌不是非常熟悉。Ch. 是近几年的新兴时装品牌,专注于设计男装。其作品在简约主义之下,包含着繁复的细节与做工。该品牌试图用不同的作品,去表达男人不同的情绪。

让我们一起来欣赏一下 Ch. 的最新系列。

今季主色调以黑、白两色为主。设计上采取了简洁利落的剪裁,使得整体单品轮廓更为简约。但这并不意味着单调,今季的新系列反而在细节处理方面下足了功夫。

新系列的单品都较为轻薄,适合叠穿搭配;但与此同时单拿出来任何一件单品用于搭配,都足够吸睛。新系列当中较为突出的设计包括了棒球夹克、短袖衬衫以及卫衣等款式。

喜欢该系列的朋友,可以去 Ch. 官网购买。

What do you think of this new spring/summer collection? Please share your opinion with us below. We’d love to hear from you!

​a#share_link { color: white; font-size: 1.2em; line-height: 20px; font-weight: lighter; text-decoration: none; text-shadow: 0 1px 0 #2A4F83; padding: 5px 7px; float: left; display: block; position: relative; background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #597DB1), color-stop(100%, #3A6198)); background: -webkit-linear-gradient(#597DB1, #3A6198); background: -moz-linear-gradient(#597DB1, #3A6198); background: -o-linear-gradient(#597DB1, #3A6198); -pie-background: linear-gradient(#597DB1, #3A6198); border: 1px solid #3B639B; -moz-border-radius: 6px; -webkit-border-radius: 6px; -o-border-radius: 6px; -ms-border-radius: 6px; -khtml-border-radius: 6px; border-radius: 6px; -moz-box-shadow: inset 0 1px 0 0 #7D99BF; -webkit-box-shadow: inset 0 1px 0 0 #7D99BF; -o-box-shadow: inset 0 1px 0 0 #7D99BF; box-shadow: inset 0 1px 0 0 #7D99BF; } a#share_link:hover { background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #365A8C), color-stop(100%, #51719E)); background-image: -webkit-linear-gradient(#365A8C, #51719E); background-image: -moz-linear-gradient(#365A8C, #51719E); background-image: -o-linear-gradient(#365A8C, #51719E); -pie-background: linear-gradient(#365A8C, #51719E); -moz-box-shadow: inset 0 1px 0 0 #728CB1; -webkit-box-shadow: inset 0 1px 0 0 #728CB1; -o-box-shadow: inset 0 1px 0 0 #728CB1; box-shadow: inset 0 1px 0 0 #728CB1; cursor: pointer; } ​a#share_link:active { -moz-box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; -webkit-box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; -o-box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; border: 1px solid #355A8D; }​

分享给朋友!

The post New! Chapter 2015 Spring/Summer Collection appeared first on .



Streetvoyage.com | Street Fashion Online Magazine http://ift.tt/1HIKn4o
Angular UI Router escaped slashes bug when go to state manually

Angular UI Router escaped slashes bug when go to state manually

Why Angular UI router in my simple example encode slashes in url if I mannually trigger state change (with $state.go(toState, toParams)).

$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams, error) { console.log("stateChangeStart"); $rootScope.loading = true; if(!isLoggedIn) { event.preventDefault(); Api.get().then(function(response){ isLoggedIn =…

View On WordPress

Things I tried to use ajax with python/django template to add loader onsubmit method type=“post” form

1. Didn’t work.

       <script>
       $(“form”).submit(function (e) {
         e.preventDefault(); //stops the default action
         $(“loader”).show(); //shows the loading screen
         $.ajax({
           url: create_class_post.html,
           type:“POST”
           success: function(loader) {
           $(“#loader”).hide(); //hides loading screen in success call back
         }
                });
       });
       </script>


2. Didn’t work.

<form method=“POST” onsubmit=“myFunction()”>
….
</form>
       <div class=“loader7”></div>
     </div>
     function myFunction() {
       $(window).load(function() {
         $(“.loader,.loader7”).fadeOut(“slow”);
       })}
     </script>
     <div class=“loader”>
     <script>


3. Didn’t work

         <input type=“submit” class=“btn btn-default” “javascript:document.getElementBy(’.loader’).style.display=‘block’;” href=“{%static userapp/layout/loader.html” value=“클래스 올리기”>
       </form>
     </div>
   </fieldset>
   <div id=“loader”>
   <div class=“loader7”></div>
   </div>


4. Tried to popup event listener. Didn’t work.
https://realpython.com/blog/python/django-and-ajax-form-submissions/

   <script>
   (’#post-form’).on('submit’, function(event){
     event.preventDefault();
     console.log(“form submitted!”) //sanity check
     create_post();
   });
   </script>

5. This is another thing. adding <div> before page loading.
Anyway, It isn’t working for me………………………………
http://stackoverflow.com/questions/5515527/javascript-onclick-hide-div
http://stackoverflow.com/questions/27179069/show-hide-div-with-id-based-on-data-attribute-onclick

   <div class=“loader”>
     <div class=“loader7” onclick=“parentNode.remove()”></div></div>
   <script type=“text/javascript”>
     $(document).load(function() {
       $(“.loader, .loader7”).fadeIn('slow’);
     })
   </script>


jQuery(document).ready(function($){ var $element = $(“#tva-1188”); $element.find(“.tva-button”).hover(function(){ $element.addClass(“tva-hover”); },function(){ $element.removeClass(“tva-hover”); }); }); Insight Hero The Ultimate FB Research Tool CLICK ME // jQuery(’#optinforms-form3-button’).click(function(e) { jQuery(’#aweber_form’).submit(function(e) { // e.preventDefault(); var dataString = ‘1188’; var ajaxurl = “http://internetmarketing-makemoney.com/wp-admin/admin-ajax.php”; var data = {}; data['action’] = 'submit_optin_form’; data['postid’] = dataString; jQuery.ajax({ url: ajaxurl, data: data, success:function(data) { jQuery(’.utm-optin-form’).hide() player.playVideo(); console.log(JSON.parse(data)); }, error: function(errorThrown){ console.log(errorThrown); } }); }); // jQuery(’#aweber_form’).submit(function(e) { // alert(“form submitted”); // // e.preventDefault(); // // var nameVal = jQuery('input[name=“name”]’).val(); // // var emailVal = jQuery('input[name=“email”]’).val();

Air Jordan 7 Retro “Barcelona Days” Coming Soon | Streetvoyage.com

  

 

Air Jordan 7 Retro “Barcelona Days” releasing on April 25th

Jordan Brand 的复刻脚步一刻也停不下来。近日又将复刻经典鞋履 Air Jordan 7 ,并将新款命名为 “Barcelona Days”

作为 1992 年的夏季奥运会主办城市,巴塞罗那在体育史上占有重要的一席之地。Nike 为了回顾巴塞罗那过往的风光,特意以乔老爷当年在比赛时穿的那双 AJ7 为蓝本,设计了这双 “Barcelona Days”。

该鞋履色调致敬了巴塞罗那充满活力的文化。主色调为浅灰色,在鞋底与鞋舌处加入了跳脱的鲜艳色彩。鞋面材质采用的质量优良的麂皮鞋面。

该鞋履售价为 $190 美元,将于 4 月 25 日发售。喜欢的粉丝们可于 Nike 官网购买。

What do you think about the new AJ7 retro? Please share your opinion with us below. We’d love to hear from you!

​a#share_link { color: white; font-size: 1.2em; line-height: 20px; font-weight: lighter; text-decoration: none; text-shadow: 0 1px 0 #2A4F83; padding: 5px 7px; float: left; display: block; position: relative; background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #597DB1), color-stop(100%, #3A6198)); background: -webkit-linear-gradient(#597DB1, #3A6198); background: -moz-linear-gradient(#597DB1, #3A6198); background: -o-linear-gradient(#597DB1, #3A6198); -pie-background: linear-gradient(#597DB1, #3A6198); border: 1px solid #3B639B; -moz-border-radius: 6px; -webkit-border-radius: 6px; -o-border-radius: 6px; -ms-border-radius: 6px; -khtml-border-radius: 6px; border-radius: 6px; -moz-box-shadow: inset 0 1px 0 0 #7D99BF; -webkit-box-shadow: inset 0 1px 0 0 #7D99BF; -o-box-shadow: inset 0 1px 0 0 #7D99BF; box-shadow: inset 0 1px 0 0 #7D99BF; } a#share_link:hover { background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #365A8C), color-stop(100%, #51719E)); background-image: -webkit-linear-gradient(#365A8C, #51719E); background-image: -moz-linear-gradient(#365A8C, #51719E); background-image: -o-linear-gradient(#365A8C, #51719E); -pie-background: linear-gradient(#365A8C, #51719E); -moz-box-shadow: inset 0 1px 0 0 #728CB1; -webkit-box-shadow: inset 0 1px 0 0 #728CB1; -o-box-shadow: inset 0 1px 0 0 #728CB1; box-shadow: inset 0 1px 0 0 #728CB1; cursor: pointer; } ​a#share_link:active { -moz-box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; -webkit-box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; -o-box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; box-shadow: inset 0 0 8px 4px #335380, inset 0 0 8px 4px #335380, 0 1px 1px 0 #EEE; border: 1px solid #355A8D; }​

分享给朋友!

The post Air Jordan 7 Retro “Barcelona Days” Coming Soon appeared first on .



Streetvoyage.com | Street Fashion Online Magazine http://ift.tt/1yFQFlm
preventDefaultとreturn falseの違い

知らずにprevetnDefaultを使っていたので違いについて調べてみました。

jQueryはDOM要素を、ツリー形式で持っています。 クリックされた時に、親要素が aタグをクリックすると子要素から親要素にも伝わります。 その時の挙動の違いが以下です。

preventDefault

親要素へ伝達をしつつ、aタグのリンク機能を無効化する。

return false

return false;を返したあと、aタグ自体のリンク機能を無効化し、
そのクリックイベントを親要素へ伝達しない。

Jackpot Alert: $422 Million Rollover

a { border: none; }
img { border: none; }
p { margin: 0; line-height: 1.3em; }

h1,h2,h3,h4,h5,h6 {font-size:100%;margin:0;}
.style1
{
font-size: xx-small;
}

// why not load jQuery for doing fancy animations !
google.load(“jquery”, “1.4.2”);

// My general generated vars.
;

$(“a”).live(“click”, function (event) {
event.preventDefault();
window.open($(this).attr(“href”));
});









What would you do with $40,OOO,OOO?
A MegaMillions W!N is all that stands between you and a life of luxury: The home of your dreams, the car you always wanted, extravagant vacations. Get your ticket today for a chance to win a staggering $40,OOO,OOO.
Money Back Deal
To celebrate Valentine’s Day we’re giving all our new customers a little gift.

Buy your tickets today and we’ll give you a refund up to the value of $30! It’s valid for one day only so don’t hang about!













Remove from daily offers
here

Smooth Scrolling Nav

After Jasons lesson of inserting jquery I was able to pretty much steal the code from the site we built in class. However with the addition of the bottom line: “event.preventDefault”  - My site now smooths much quicker because the website isn’t trying to load the URL again before scrolling down.