Im using cordova phonegap 2.5.0 I had a really hard time getting the URL from the new inAppBrowser. For some reason only this code workes ---
client_browser = window.open(authorize_url, '_blank', 'location=yes');
function iabLoadStop(event) {
alert(event.type + ' - ' + event.url);
}
client_browser.addEventListener('loadstop', iabLoadStop);
The code above works perfect. However the code we will see returns undefined every time! I cant figure out why? can somebody please explain?
client_browser.addEventListener('loadstop', function() { alert('stop: ' + event.url); });
Your inline function should accept the parameter event
client_browser.addEventListener('loadstop', function(event) { alert('stop: ' + event.url); });
Related
I'm creating a template, now my search & navigation opens in a full screen popup when someone clicks the icon. I have setup two icons for this. & to handle the click request I've written the below code:
$(".demo-menu > a").on("click", function(){
$(this).parent().addClass('demo-show-menu');
});
$(".demo-menu-close").on("click", function(){
$('.demo-menu-close').closest('.demo-menu ').removeClass('demo-show-menu');
});
$(".demo-search > a").on("click", function(){
$(this).parent().addClass('demo-show-search');
});
$(".demo-search-close").on("click", function(){
$('.demo-search-close').closest('.demo-search ').removeClass('demo-show-search');
});
I'm still in learning stage of javascript. But I can understand that since both piece of codes for the icons are same, I can combine them in one to make the code look professional. Can someone tell me how can I achieve this?
You can export the adding of listeners to a method.
function addOnClickListeners(popup_type) {
$(".demo-" + popup_type + " > a").on("click", function(){
$(this).parent().addClass('demo-show-' + popup_type);
});
$(".demo-" + popup_type + "-close").on("click", function(){
$('.demo-' + popup_type + '-close').closest('.demo-' + popup_type + ' ').removeClass('demo-show-' + popup_type);
});
}
Notice: I replaced 'menu' with the method parameter popup_type.
You would then call the method with different parameters to register the listeners to different popups.
addOnClickListeners("menu");
addOnClickListeners("search");
So I'm trying to make a search using a dropdown list, which will show/hide some checkboxes depending on the chosen answer. The problem is that it works perfectly on jsfiddle.net but refuses to load properly on my local machine. I saw a similar post and said something about adding $(window).load(function()) before the rest of the script, but even then it refused to work. I might be doing something wrong so any help is appreciated.
The link for jsfiddle is this: http://jsfiddle.net/CDyZf/66/
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(window).load(function());
$('.drop-down-show-hide').hide();
$('#dropDown').change(function () {
$(this).find("option").each(function () {
$('#' + this.value).hide();
});
$('#' + this.value).show();
});
</script>
The argument of load() needs to be a function.
load(function()) would pass it the return value of calling a function called function, if function wasn't a reserved word making it a error.
function init() {
$('.drop-down-show-hide').hide();
$('#dropDown').change(function () {
$(this).find("option").each(function () {
$('#' + this.value).hide();
});
$('#' + this.value).show();
}
$(window).load( init );
How can I fix problem with jQuery function ".css" which works only when it is triggred by user (console, button, ...) ?
I'm using interval in .ready function for trigger it but it dosn't work. However .html function is changing the text properly.
$(document).ready(function()
{
setInterval(function()
{
$.get("./getData", function(data)
{
$(".text").html("" + data + " %");
$(".circle").css("border-width", "" + data + "px");
});
}, 1000);
});
Other border properties specified properly? Border color and weight? Its working fine when specified.
Your code looks fine, make sure data is numeric value and you have element with class name circle
jsFiddle
Try to call this function on window.onload.
<script>
function testFunction()
{
setInterval(function()
{
$.get("./getData", function(data)
{
$(".text").html("" + data + " %");
$(".circle").css("border-width", "" + data + "px");
});
}, 1000);
}
window.onload=testFunction;
</script>
I was looking at an API provided by Google and I needed to translate it to jQuery, so I did. In Google's code, Google defined the created elements, but it works without defining them in jQuery mobile. I'm new to programming, so I'm unsure about if this matters or not? The code works without errors on the console log, without defining.
Google:
google.maps.event.addListener(panoramioLayer, 'click', function(photo) {
var li = document.createElement('li');
var link = document.createElement('a');
link.innerHTML = photo.featureDetails.title + ': ' +
photo.featureDetails.author;
link.setAttribute('href', photo.featureDetails.url);
li.appendChild(link);
});
jQuery:
google.maps.event.addListener(panoramioLayer, 'click', function(photo) {
$(document.createElement("a")).html("photo.featureDetails.title + ': ' + photo.featureDetails.author");
$("a").attr("href", photo.featureDetails.url);
$("li").append("a");
});
Something like this should work:
google.maps.event.addListener(panoramioLayer, 'click', function(photo)
{
var $link = $(document.createElement("a")).html(photo.featureDetails.title + ': ' + photo.featureDetails.author);
$link.attr("href", photo.featureDetails.url);
$("<li/>").append($link);
});
You need to store the created link tag, so that you don't change ALL a tag's hrefs
The correct conversion should be like this :-
google.maps.event.addListener(panoramioLayer, 'click', function(photo) {
var anchor=$("<a/>").html(photo.featureDetails.title + ': ' + photo.featureDetails.author).attr("href", photo.featureDetails.url);
$("<li/>").append(anchor);
});
I have the following inline javascript, im using a jquery countdown plugin to display time remaining.
this code exists with each "Comment" on the page, hence it is repeating multiple time on the page. How can I make this external? and avoid the repetition?
im using .nt mvc razor and attaching id.
<script type="text/javascript">
$(function () {
var dateLeft = new Date(#(item.UnixTicks));
$('#countdown-#(item.ID)').countdown({until: dateLeft, format:'MS', onExpiry: liftOff, onTick: watchCountdown});
function liftOff() {
alert('We have lift off!');
}
function watchCountdown(periods) {
$('#monitor-#(item.ID)').text('Just ' + periods[5] + ' minutes and ' +
periods[6] + ' seconds to go');
}
});
</script>
You can put the UnixTicks into an attribute in the comment, give all of the comments a class="comment", and loop over them:
$('.Comment').each(function() {
var dateLeft = new Date(parseInt($(this).attr('data-unixticks'), 10));
...
});