Load modules with Lab.js - javascript

Why ReferenceError: $ is not defined ?
<script src="LAB.js"></script>
<script>
$LAB.script("jquery/jquery.js")
</script>
</body>
<p><?php echo mt_rand(89,161464) ?></p>
<script>
//Uncaught ReferenceError: $ is not defined
$(document).ready(function(){
console.log('reddy');
$("p").css('color','red');
})
</script>
But work:
window.onload=function(){
$("p").css('color','red');
}
2.item1.js
var interface={name:'interface'};
item2.js
interface.hu={name:'int'};
$LAB.script("item2.js").wait();
$LAB.script("item1.js");
//Uncaught ReferenceError: interface is not defined
Help

try :
$LAB.script("jquery/jquery.js").wait(function () {
if( window.jQuery ) { //is jquery loaded
$("p").css('color','red');
}
});
OR
$LAB.script("jquery/jquery.js").wait(function () {
$(document).ready(function() {
$("p").css('color','red');
}
});
See more:: LABjs Doc

Related

jQuery Freichat conflict

I'm trying to integrate the freichat module to my website, but i got this error
TypeError: $ is not a function
I tried to encapsulate it with an anonymous function passing jQuery as the parameter , like this:
(function($) {
$(document).ready(function() {
// my custom code
});
})(jQuery);
I also put jQuery instead of $ and got the below issue:
jQuery is undefined
Any idea regarding this?
Codes:
<script src="JS/jquery.js"></script>
<script language="javascript">
$(document).ready(function () {
$("#lawyerrr").select2();
$("#fromm").select2();
$("#too").select2();
});
</script>
<?php
$ses = null; // Return null if user is not logged in
if (!empty($_SESSION['user'])) {
$loggedUser = $_SESSION['user'];
}
if(isset($loggedUser['idd']))
{
if($loggedUser['idd'] != null) // Here null is guest
{
$ses=$loggedUser['idd']; //LOOK, now userid will be passed to FreiChat
}
}
//echo $ses;
if(!function_exists("freichatx_get_hash")){
function freichatx_get_hash($ses){
if(is_file("C:/wamp64/www/freichat/hardcode.php")){
require "C:/wamp64/www/freichat/hardcode.php";
$temp_id = $ses . $uid;
return md5($temp_id);
}
else
{
echo "<script>alert('module freichatx says: hardcode.php file not
found!');</script>";
}
return 0;
}
}
?>
<script type="text/javascript" language="javascipt" src="http://localhost/freichat/client/main.php?id=<?php echo $ses;?>&xhash=<?php echo freichatx_get_hash($ses); ?>"></script>
<link rel="stylesheet" href="http://localhost/freichat/client/jquery/freichat_themes/freichatcss.php" type="text/css">
Add JQuery library to your document:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Difference between "alert(a)'' and ''alert(a);var a =1;'' in javascript?

<script type="text/javascript">
alert(a);
</script>
Console log shows : "Uncaught ReferenceError: a is not defined";
<script type="text/javascript">
alert(a);
var a = 1;
</script>
at the middle of the browse, Log shows: "undefined"
How does this code run in js and what causes this difference
in this code
<script type="text/javascript">
alert(a);
var a = 1;
</script>
var a ; is hoisted to the top and it becomes
<script type="text/javascript">
var a;
alert(a);
a = 1;
</script>
so by the time a was alerted, it was undefined
In this code
<script type="text/javascript">
alert(a);
</script>
a was not defined at all, so it gave an error "Uncaught ReferenceError: a is not defined"

PHP Laravel #include javascript variable not found

Laravel how o can use javascript variables from include file? I try this below example but variable not found..
includeFile.blade.php
<script>
$(document).ready(function () {
var myMar = "Hello world";
});
</script>
default.blade.php
#include('layouts.includeFile')
<script>
$(document).ready(function () {
alert(myVar); //Here myVar is undefined
});
</script>
myVar must be declared outside the function to be visible in another functions
includeFile.blade.php
<script>
var myVar; // Create global variable
$(document).ready(function () {
myVar = "Hello world";
});
</script>
default.blade.php
#include('layouts.includeFile')
<script>
$(document).ready(function () {
alert(myVar);
});
</script>
It is because myMar is private in includeFile.blade.php. Try something like this
includeFile.blade.php
<script>
$(document).ready(function () {
var myMar = "Hello world";
#yield('doc_ready')
});
</script>
default.blade.php
#extends('includeFile')
#section('doc_ready')
alert(myVar); //Here myVar is defined
#stop
Or just remove var
<script>
$(document).ready(function () {
myMar = "Hello world";
});
</script>
And also you have typo in includeFile it is myMar and in default it is myVar

Uncaught TypeError: number is not a function : While create dojo class

I am learning dojo
i have created a dojo class using declare as below
require(
["dojo/_base/declare"],function(declare){
return declare(null,{
constructor : function(){
alert("done");
}
});
});
and this is save in /learnDojo/root/test.js
and the index.html as below
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script type="text/javascript">
var dojoConfig = {
async: false,
parseOnLoad: false,
isDebug : true,
packages:[
{ name:"base",
location:"/learnDojo/root"
}]
};
</script>
<script src="//localhost:8080/dojo1.9.0/dojo/dojo.js" type="text/javascript"></script>
<script type="application/javascript">
require(["base/test","dojo/_base/declare","dojo/domReady!"],function(test,declare){
var test = new test();
});
</script>
<body>
</body>
</html>
i have the index.html in the /learnDojo which is in webapps folder of the tomcat
When i load this from browser i am getting the error "Uncaught TypeError: number is not a function"
Why? any issue with code or dojo syntax for creating the class
When defining Dojo modules you should be using define(), not require(). So your /learnDojo/root/test.js file would look like this:
define(
["dojo/_base/declare"],function(declare){
return declare(null,{
constructor : function(){
alert("done");
}
});
});
The reason for this is that define() will actually return the object/value you returned in your callback (in this case the return declare(...) statement. A require() however will not return that value, causing strange errors.

JavaScript isn't working on my server

This code is working on other test environment but not on mine.
Do you know why?
I am using Amazon EC2 and Cotendo CDN.
The result I am getting is a blank page.
Thanks in advance!
<html>
<head>
<title>Geo Test</title>
<script type='text/javascript' src='http://www.101greatgoals.com/wp-includes/js/jquery/jquery.js?ver=1.7.1'></script>
<script>
$(document).ready( function() {
$.getJSON( "http://smart-ip.net/geoip-json?callback=?",
function(data){
console.log(data);
var c = data.countryCode;
if(c=="US" || c=="US" ){
document.getElementById('ddd').innerHTML = 'US'; } else {
document.getElementById('ddd').innerHTML = 'Not US';}
/*
this service needs ip
var ip = data.host;
alert(ip);
$.getJSON( "http://freegeoip.net/json/"+ip,
function(data){
console.log(data);
}
);*/
}
);
});?
</script>
</head>
<body>
<div id="ddd"></div>
</body>
</html>
Change this line:
$(document).ready( function() {
to that:
jQuery(document).ready( function($) {
It's necessary, because inside http://www.101greatgoals.com/wp-includes/js/jquery/jquery.js?ver=1.7.1 you've already got a call of jQuery.noConflict(); , so jQuery is not accessible by using the $
...and also remove the ? (see Pointy's comment above)

Categories

Resources