unobtrusive-javascript

JQuery: Why Unobtrusive JavaScript / Document Ready function rather than OnClick event?

北慕城南 提交于 2019-11-27 14:11:18
问题 I've just started looking at JQuery. I don't have any AJAX in my web application at present. My existing JavaScript in my HTML looks like: <form ...> <p>Find what? <input ...></p> <div class="ButtonArray"> <a href="AddRecord.ASP&Action=ADD">ADD</a> <a href="#" onClick="return MyFormSubmit();">FIND</a> </div> </form> This displays Buttons for [Add] and [Find]. For Find it is necessary to Submit the form, and I use MyFormSubmit() which validates the data, adds a visual effect to the page, and

Using Google Analytics asynchronous code from external JS file

强颜欢笑 提交于 2019-11-27 12:39:39
I'm trying to add the asynchronous version of the Google Analytics tracking code to a website. I'd like to keep the JavaScript in a separate file, and call it from there. Here's what I've currently got in my .js file: function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } } function loadtracking() { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXX-X']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type =

How to find all Siblings of currently selected object

大兔子大兔子 提交于 2019-11-27 10:53:28
问题 What is the perfect way to find all nextSiblings and previousSiblings in javascript. I tried few ways but not getting accurate solution. if any element is selected, I need to get length of all next siblings excluding white-space, any spaces or line-breaks Also I don't want to use jquery for this, i specifically looking something from java script Please advice 回答1: I'll assume that this takes place inside an event handler where this is a reference to the targeted element whose siblings you

How Does Rails 3's “data-method='delete'” Degrade Gracefully?

China☆狼群 提交于 2019-11-27 01:14:10
问题 Rails 3 does some cool stuff to make Javascript unobtrusive, so they've done things like this: = link_to "Logout", user_session_path, :method => :delete ..converts to <a href="/logout" data-method="delete" rel="nofollow">Logout</a> But it just occurred to me.. When I turn off javascript the method isn't DELETE anymore, it's GET as expected. So are there plans to, or is there some way to, allow these data- attributes to degrade gracefully, so that link still is a DELETE request? 回答1: The

How to check browser for touchstart support using JS/jQuery?

允我心安 提交于 2019-11-26 21:53:36
In an attempt to follow best practices, we're trying to use the proper JavaScript/jQuery events according to what device you are using. For example, we're building a mobile site that has an tag that will have an onclick or touch event. In the case of an iPhone, we'd like to use the "touchstart" event. We'd like to test if their device supports "touchstart" before we bind that handler to the object. If it doesn't, then we will bind "onclick" instead. What is the best way to do this? CMS You can detect if the event is supported by: if ('ontouchstart' in document.documentElement) { //... } Give a

RequiredAttribute with AllowEmptyString=true in ASP.NET MVC 3 unobtrusive validation

蹲街弑〆低调 提交于 2019-11-26 19:25:01
问题 If i have [Required(AllowEmptyStrings = true)] declaration in my view model the validation is always triggered on empty inputs. I found the article which explains why it happens. Do you know if there is a fix available? If not, how do you handle it? 回答1: Note: I'm assuming you have AllowEmptyStrings = true because you're also using your view model outside of a web scenario; otherwise it doesn't seem like there's much of a point to having a Required attribute that allows empty strings in a web

Find user location using jQuery/JS (without google geolocation api)?

℡╲_俬逩灬. 提交于 2019-11-26 18:59:31
问题 Is there a way to find a clients location on a website using just jQuery. For example, if a user comes to my site, how could I find out what their approximate location is just using jQuery. For example, if a user came from San Francisco CA it would have some type identifier to let me know the user came from San Francisco CA. I wouldn't really need their exact location just the county or general area of origin. edit: How is the information on http://flourishworks-webutils.appspot.com/req

MVC unobtrusive validation on checkbox not working

时间秒杀一切 提交于 2019-11-26 18:49:50
问题 I'm trying to implement the code as mentioned in this post. In other words I'm trying to implement unobtrusive validation on a terms and conditions checkbox. If the user hasn't selected the checkbox, then the input should be marked as invalid. This is the server side Validator code, I've added: /// <summary> /// Validation attribute that demands that a boolean value must be true. /// </summary> [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class

Using Google Analytics asynchronous code from external JS file

妖精的绣舞 提交于 2019-11-26 16:05:47
问题 I'm trying to add the asynchronous version of the Google Analytics tracking code to a website. I'd like to keep the JavaScript in a separate file, and call it from there. Here's what I've currently got in my .js file: function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } } function loadtracking() { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXX-X'

Difference between obtrusive and unobtrusive javascript

流过昼夜 提交于 2019-11-26 14:30:39
What is the difference between obtrusive and unobtrusive javascript - in plain english. Brevity is appreciated. Short examples are also appreciated. I don't endorse this anymore as it was valid in 2011 but perhaps not in 2018 and beyond. Separation of concerns. Your HTML and CSS aren't tied into your JS code. Your JS code isn't inline to some HTML element. Your code doesn't have one big function (or non-function) for everything. You have short, succinct functions. Modular. This happens when you correctly separate concerns. Eg, Your awesome canvas animation doesn't need to know how vectors work