How to NOT execute javascript code on specific page [duplicate] - javascript

This question already has answers here:
Conditionally load JavaScript file
(8 answers)
Closed 4 years ago.
I have specific javascript code
<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//mydomaincom.com/generic/uni.php?g=5"></script>
that work on all pages, but on /ucp.php?mode=register I dont want to execute the code. How to do this?
I made this code but not working (two script inside?)
<script>
if ( window.location.pathname == '/ucp.php?mode=register'{
} else {
<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//sk.search.etargetnet.com/generic/uni.php?g=5"></script>
}
</script>
The answer should be with PHP too, if with PHP i can make it.

Try this.
if ( window.location.pathname != '/ucp.php'){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src","//sk.search.etargetnet.com/generic/uni.php?g=5")
fileref.setAttribute("async","true")
fileref.setAttribute("data-ad-type","iframe v2.0")
fileref.setAttribute("charset","utf-8")
}

Related

(jQuery) Uncaught ReferenceError: $ is not defined in Laravel [duplicate]

This question already has answers here:
JQuery - $ is not defined
(36 answers)
Closed 6 years ago.
This post was edited and submitted for review 9 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Jquery
<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>
my code
<script type="text/javascript">
function readURL(input){
if(input.files && input.files[0]){
var reader = new FileReader();
reader.onload=function(e){
$('#showimages').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#inputimages").change(function(){
readURL(this);
});
</script>
<img src="" id="showimages">
<input type="file" name="images" id="inputimages">
and i got this error
Uncaught ReferenceError: $ is not defined
at http://localhost/project/public/user/1/edit:308:9
*line 308 -> $("#inputimages").change(function(){
Im new in js and jquery, Help me solve this error please...
I wrote the script out of the section. That was why the error occurred, see my answer.
Thankyou for everyone!
This error is solved, in .blade.php laravel i have to write
#section('script')
<script type="text/javascript">
//my code here
</script>
#endsection
To use Jquery, remember it has to import before using
Your problem is look like your Jquery is not imported. Maybe your path is incorrect or you imported it after your script.
First, check this url http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js with with your web browser. If it exist you will see a lot of javascript code there.
Then to import it from your local server.
<script src="http://localhost/project/public/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script>
//your script here.
</script>
Another choice using cdn instead:
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
//your script here
</script>
Looks like jQuery wasn't loaded when your script is exectued.
You should load jQuery before scripts that are using it.
You have JavaScript running before the page is fully loaded.
You can load another plugin which may have overwritten the $ variable.
Your JavaScript file is not being properly loaded into your page(Check you script path and try to open link directly on browser).
Make sure all javascript code is being run inside a code block such as:
$(document).ready(function () {
//your code
});
or
(function($){ })(jQuery);
or equivalent pure javascript code.
There may be two potential problem in your code.
You don't have to put your code before scripts that using it. (Check your view source from browser)
You may imported another jQuery library.
If you imported two different jQuery Library, you can solve with noConflict()
e.g
<script type="text/javascript">
j = $.noConflict();
function readURL(input){
if(input.files && input.files[0]){
var reader = new FileReader();
reader.onload=function(e){
j('#showimages').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
j("#inputimages").change(function(){
readURL(this);
});
</script>
Try my updated answer. Assumed you have main layout file.
<head>
<script src="{!! asset('plugins/jQuery/jquery-2.2.3.min.js') !!}"></script>
<script src="{!! asset('bootstrap/js/bootstrap.min.js') !!}"></script>
<script src="{!! asset('plugins/select2/select2.full.min.js') !!}"> </script>
<script src="{!! asset('plugins/slimScroll/jquery.slimscroll.min.js') !!}"></script>
<script src="{!! asset('plugins/fastclick/fastclick.js') !!}"></script>
#yield('script')
<body>
#yield('content')
<script src="{!!asset('plugins/datatables/jquery.dataTables.min.js')!!}"></script>
</body>
</head>

JSP : using java variable in Javascript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 4 years ago.
I have a JSP page called index.jsp. I have a Java variable called totalCount inside this page :
<%= int totalCount = getTotalCount();%>
Now I want to use this variable in Javascript section to generate a chart:
<script type="text/javascript">
</script>
How can I pass this Java variable inside ? Thanks.
Just assign the value to your Javascript variable.
<script type="text/javascript">
var count = '<%= totalCount %>';
</script>
But using scriptlets been highly discouraged and shift to JSTL as soon as possible.
Read : How to avoid Java code in JSP files?
As others pointed out, do not create unnecessary hidden elements on DOM. If you really want to use this variable across files declare this variable on top. Even before script includes, so that it avail across all the files.
Example:
<script type="text/javascript">
var count = '<%= totalCount %>';
</script>
<script type='text/javascript' src='js/blah.js'></script>
<script type='text/javascript' src='js/blahblah.js'></script>
Just create a hidden field on your HTML page and access that value using JavaScript.
HTML
<input id='hdn-total-count' type='hidden' value='<%=totalCount%>' />
JavaScript
var totalCount = document.getElementById('hdn-total-count').value;
jQuery (Cross browser compatible)
var totalCount = $('#hdn-total-count').val();
All of the other answers are going to work on inline and on-page JavaScript. But they are not going to work for JavaScript in external files (which are cacheable).
simply you can use
<script type="text/javascript">
var xyz=<%= getTotalCount();%>
</script>
But I dont recommend you to use java codes inside JSP.Please look for JSTL
you can write a code something like this. But it's not a clean way of doing it. Why do you want to do it? You can do it via AJAX calls.
Here is the sample code
<sciprt type="text/javascript">
var myVariable = <%=request.getAttribute("rep");%>
</script>

Javascript External Object Global? [duplicate]

This question already has answers here:
JavaScript Code Inside <script> Tag
(3 answers)
Closed 8 years ago.
I'm messing around with objects and methods, and I have this really simple example that I use to test with:
var shout = {
hello: function(variable){
console.log("Hello " + variable);
}
};
shout.hello("World");
And this works fine. However, if I place the object shout in an external file, and then run shout.hello("world"); I get nothing:
//external file: test.js
var shout = {
hello: function(variable){
console.log("Hello " + variable);
}
};
<!-- my html document -->
<script src="test.js">
shout.hello("World");
</script>
What am I doing wrong?
From MDN:
script elements with an src attribute specified should not have a script embedded within its tags.
You need two separate script tags, one to import your external script, and another to call the function, for example:
<script src="test.js"></script>
<script>
shout.hello("World");
</script>
You need two separate script tags, the content of a tag with src attribute is ignored.
<script src="test.js"></script>
<script>
shout.hello("World");
</script>

How do I load a script dynamically via Backbone? [duplicate]

This question already has answers here:
JQuery to load Javascript file dynamically
(2 answers)
Dynamically load a JavaScript file
(30 answers)
Closed 8 years ago.
Inside one of my views, I'd like to load this script:
<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>
Is it possible to do that?
Manually appending it:
$('head').append('<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>')
Using $.getScript:
$.getScript('https://domain.com').done(function () {
// loaded!
});
RequireJS:
require.config({
paths: {
"myscript": "https://domain.com"
}
});
require(['myscript'], function () {
// loaded!
});

Access a function in js [duplicate]

This question already has answers here:
Can we call the function written in one JavaScript in another JS file?
(10 answers)
Closed 8 years ago.
I have a function in my separate js file called hello().
On my page I have this js:
<script type='text/javascript'>//do hello fnc</script>
How can I get the on page script to call the function in my main js file?
Call it in the src attribute of another script tag, before referring to any of its functions in this script.
<script type='text/javascript' src='main.js'></script>
<script type='text/javascript'>
hello();
// Anything defined in previously included js files like main.js
// is now available, as well as anything defined on this page.
</script>
Load the external file first and call the function:
<script src=external.js></script>
<script>
hello();
</script>
You just need to include the external js file:
<script type="text/javascript" src="MyOtherFile.js"></script>
And then wherever in you code you want to call the funciton:
<script type="text/javascript">hello();</script>

Categories

Resources