call a function by using drupal add js - javascript

i'm debugging a drupal module, the second line drupal_add_js call a js function in uc_discounts.js
drupal_add_js(drupal_get_path('module', 'uc_discounts') . '/uc_discounts.js');
drupal_add_js('(function($){jQuery(document).ready(function () { uc_discountsOnLoad(e); })})(jQuery);', array('type' => 'inline', 'group' => JS_DEFAULT, 'scope' => 'footer'));
there is an error message in firebug, ReferenceError: uc_discountsOnLoad is not defined, anyone can figure out the problems?
** update **
i knew the problems!
the uc_discountsOnLoad if defined outside (function ($) {})(jQuery); can be called~ but it can't use jquery symbol, it is a conflict!
but in the situation , how do i call this function?
function uc_discountsOnLoad(e) {
context = $('body');
uc_discountsProcessCodes(context, e);
//Add click event listener to discounts pane button once
$("input[id*=uc-discounts-button]:not(.uc_discountsOnLoad-processed)",
context).addClass("uc_discountsOnLoad-processed").click(function(e) {
uc_discountsProcessCodes(context, e);
//Return false to prevent default actions and propogation
return false;
});
}
if define the uc_discountsOnLoad(e) outside the (function ($) {})(jQuery);, $('body') will got an error!

The uc_discountsOnLoad(e) function cannot be found.
Check uc_discounts.js to see if the function actually exists.
uc_discounts.js gets loaded after the function is called? (Check your page source)

Related

AngularJS return value in the scope from an addEventListener

I have an AngularApp in an iframe and I have a controller with this code to get a click event from the iframe container (main window):
$window.addEventListener('message', function(e) {
$scope.$apply(function() {
console.log(e.data.url); // http://<something>
$scope.widget.pageUrl = e.data.url;
});
});
console.log($scope.widget.pageUrl); // undefined
Everything works fine except I cannot get the $scope.widget.pageUrl variable filled. It remains undefined outside the $window.addEventListener.
Any hint?
This should also work:
$window.addEventListener('message', function (e) {
console.log(e.data.url); // http://<something>
$scope.widget.pageUrl = e.data.url;
$scope.$apply();
});
console.log($scope.widget.pageUrl);
Yaa that's because javascript have function level scoping which means that you need to pass e into the function that you supply to the $scope.$apply otherwise this function will not have event object.
doing this should help.
$scope.$apply(function(e) {
console.log(e.data.url);
$scope.widget.pageUrl = e.data.url;
});
I have not tested it though.

I do not see a variable in Javascript?

In the course of development of JS, faced with such a situation. My code:
Object.prototype.show = function () {
$('span[data-search]').on('click', function (evt) {
var data_search = $(this).data('search');
$('body').on('shown.bs.modal', '.modal', function () {
$(this).find('button.synConfirm').on('click', function(){
console.log(data_search); // Not see variable
});
});
}):
});
Ie if you click on the span opens a modal window (Bootstrap 3). Then when you click on the button in the modal window, I should have access to the variable data-search announced in the beginning of the method, but I do not have access to it ...
Question: Why? After I identified her to call console.log (), .. How do I get it?

e.preventDefault(); undefined is not a function

I'm getting the following error that is saying my e.preventDefault(); ---> "e." undefined is not a function when clicking <button class='url_qry_add' onclick='url_qry_add(this);'>. The function itself is defined before the end of my </body> and I have only invoked jQuery once.
The function structure is as follows:
var url_qry_add = function ( e ) {
e.preventDefault();
...
};
It used to be:
$( "ul.url_qry" ).on( "click", "li .url_qry_add", function ( e ) {
e.preventDefault();
...
});
But subsequent buttons added dynamically afterwards were not being picked up.
So I've been trying to figure out how to go about it and decided I should try converting the problem function to a named "invokable" function and putting the call in manually with the onclick='..' into the buttons that exist before and after dynamic creation.
Like I say, the error must be in the way I've created the function or the way I'm calling it. The error can't be to do with the order of files and I have not accidentally nested the function within another function or a document.ready.
What am I doing wrong?
<button class='url_qry_add' onclick='url_qry_add(event);'>
var url_qry_add = function (e) {
console.log(typeof e.preventDefault); // function
};
Update:
I'll try clarify how it works "internally", when we add attributes to function url_qry_add "inside" it looks like this:
document.querySelector('.url_qry_add').addEventListener('click', function (event) {
(function (event) {
url_qry_add(event, this, $(this));
}).call(event.target, event);
});
var url_qry_add = function (event, element, $jElement) {
console.log(event);
console.log(element);
console.log($jElement);
};
Hence, we have variable "event" (event object, where we have method preventDefault and so on), and "this" (current element). I hope that this explanation will help you understand where we get variable "event".

How to call function from another file with requireJS

I have two files - main, and events. I'm trying to call some function from one file to another.
So, this is how it looks:
events
require(['app/main'], function(call) {
// click event respond test
document.body.addEventListener("click", function(e) {
var target = e.target;
if (target.hasClass === "call"){
functionCall()();
}
});
});
main
define(["jquery"], function() {
// Call
var box = $('.box');
return function functionCall(){
box.addClass('visible');
}
});
What is wrong, can anyboyd help?
main:
define(["jquery"], function($) {
var main = {
functionCall: function(){
$('.box').addClass('visible');
}
}
return main;
});
events:
require(['jquery','app/main'], function($, main) {
$('body').on('click', function () {
if($(this).hasClass('call')){
main.functionCall();
}
});
});
One way is to add this code where you need to make call to function:
require('pathToModuleOrModuleName').functionYouWantToCall()
But if you have module defined or required in the beggining (as 'main' in the events), then in place where call to function needed just add:
call.functionName();
Unless my eyes deceive me the simplest change to make to your code would be to replace this:
functionCall()();
with this:
call();
since the function that the main module returns is imported as call in your events module, because that's how you name it in the callback passed to define.
Firstly your code has some basic problems
In the following code
define(["jquery"], function() {
Where are you referring the query inside the function definition.
I think you should first map the jquery defined into the function declaration like below
define(["jquery"], function($) {
Secondly, what is the () doing after the calling function?
if (target.hasClass === "call"){
functionCall()();
}
Remove the trailing () from that call. It should just be functionCall();

Calling a function from an html document will not generate an alert

Hi all thanks for taking a look.
I am trying to call a javascript function when I click on the update button.
Here is the javascript
var text2Array = function() {
// takes the value from the text area and loads it to the array variable.
alert("test");
}
and the html
<button id="update" onclick="text2Array()">Update</button>
if you would like to see all the code check out this jsfiddle
http://jsfiddle.net/runningman24/wAPNU/24/
I have tried to make the function global, no luck, I can get the alert to work from the html, but for some reason it won't call the function???
You have an error in the declaration of the pswdBld function in your JavaScript.
...
var pswdBld() = function() {
---^^---
...
This is causing a syntax error and avoiding the load of your JavaScript file.
See the corrected version.
Also, you may consider binding the event and not inlining it.
<button id="update">Update</button>
var on = function(e, types, fn) {
if (e.addEventListener) {
e.addEventListener(types, fn, false);
} else {
e.attachEvent('on' + types, fn);
}
};
on(document.getElementById("update"), "click", text2Array);​
See it live.
In your fiddle, in the drop-down in the top left, change "onLoad" to "no wrap (head)"
Then change
var text2Array = function()
var pswdBld() = function()
to
function text2Array()
function pswdBld()
and it will alert as expected.
You have a syntax error in the line below..
var pswdBld() = function
^--- Remove this
supposed to be
var pswdBld = function
Also make sure you are calling this script just at the end of the body tag..
Because you are using Function Expressions and not Function Declaration
var pwsdBld = function() // Function Expression
function pwsdBld() // Function Declaration
Check Fiddle

Categories