Using PHP in a JavaScript Function in HTML - javascript

So I declared a variable empty in external JavaScript file which I am sourcing in my main HTML page and because I want to load that variable from PHP I am doing this but it doesn't seem to work.
<script src="./assets/js/script.js"></script>
<script type="text/javascript">var APIKey = <?php echo $API; ?></script>
script.js is the one having empty global variable like this:
var APIKey = "";
I have already declared $API in PHP and I know it's working because I tried echoing it as text and it works but for some reason it doesn't in script. Please help.
Thanks!

So it took a bit time to figure out but the mistake was pretty simple.
All I had to do was REMOVE this from script.js
var APIKey = "";
The problem was that I was defining it 2 times and I don't think that's supported for some reason but that worked for me so if anyone else has the same issue, it'll work for you as well. Have a good day and keep on coding.

Related

changing a js file location only when the source-code is showed

i'm trying to change a js file location when a visitor shows the source-code
, depending on that :
javascript functions don't work when the visitor shows the source-code
.
my idea is creating a file , put a javascript code to delete the file , in this situation the file won't be deleted if someone showed the source-code :
$check="$ip-$views-$id";
fopen("$check.txt","w"); //CREATING A FILE
// DELETING THE FILE ABOVE BY JAVASCRIPT , IT WON'T BE DELETED IF SOMEONE ENTERED VIA THE SOURCE-CODE MODE
?>
<div id="countno"></div>
<script type="text/javascript">
$('#countno').load('del.php?name=<?echo $check;?>&location=' + ' #countno');
</script>
<?
if (file_exists("$check.txt")) { //IF SOMEONE SHOWED THE SOURCE CODE
$new_location="fake_location.js";
}
else{
$new_location=$old_location;
}
?>
<script src="<?echo $new_location;?>"></script>
the problem now , is that the file_exists php function shows that the file still exists even though it was already deleted by the javascript code .
the file_exists function was executed before the javascript code !
any help / solution to make that php function check the file after that javascript code ? i know it's kinda impossible but it worth it !
What you are describing is not possible. php is a server side language while javascript is a client side language. Thus, the PHP on your page will always execute before the Javascript on your page executes. There is no way to make your Javascript execute first when you have it this way.
Instead, what you could do is to separate the PHP and Javascript. Have your file_exists check in another page. e.g. check.php.
if (file_exists("$check.txt")) {
echo "fake_location.js";
} else {
echo $old_location;
}
Then use an ajax call to make a request to check.php, and load your other script depending on what check.php outputs.
<script>
$.ajax({
method: "GET",
url: "check.php"
})
.done(function(script) {
$("#check").attr("src", script);
});
</script>
<script id="check"></script>
However, if your goal is to prevent people from figuring out where your javascript is located, it is not possible. Most browsers nowadays can inspect the HTML as it changes. They can see the loaded script without having to load the source separately.

Calling Javascript function in an external file through php file?

Javascript code (In another file in the same directory):
function js(str)
{
alert(str);
}
PHP code(in current file):
<?php
echo '<script type="text/javascript" src="C:\xampp2\htdocs\ARD\call_jsfunc_diff_page.js"> </script>';
echo '<script>js("hello!!")</script>';
?>
I have checked on many links on the internet, i think i'm doing the right way, but the javascript function js(str) doesn't get called !!
Can somebody help me please ??
Before you edited the question: You called the function js but you are trying to call the func function.
Access to local hard disks is also problematic. Use a relative URI and access the .js over HTTP instead.

How do I rotate an ad on a page loading the list of ads from an external file using JavaScript?

I'm trying to load a variable from a file using javascript. I've found some examples but I can't seem to make it work and could really use some help on getting my syntax right.
Basically, I want to load a random ad image on a page, but I would like the list of ads to be pulled from a file. Currently I'm loading the images using the following script which I found on the internet:
<script type="text/javascript">
var picPaths = [
'/images/ad-1.jpg',
'/images/ad-2.jpg',
'/images/ad-3.jpg',
'/images/ad-4.jpg'
]
var oPics = [];
for(i=0; i < picPaths.length; i++){
oPics[i] = new Image();
oPics[i].src = picPaths[i];
}
curPic = Math.floor(Math.random()*oPics.length);
window.onload=function(){
document.getElementById('imgRotator').src = oPics[curPic].src;
}
</script>
I have been trying to get the picPath variable value to load from a file (instead of stating it in the code). I found some code here on stackoverflow and tried adjusted it to the following:
var picPaths = new XMLHttpRequest();
picPaths.open('GET', '/images/liveimages.inc');
picPaths.send();
I also created the file /images/liveimages.inc which containts the following:
'/images/ad-1.jpg',
'/images/ad-2.jpg',
'/images/ad-3.jpg',
'/images/ad-4.jpg'
But, alas, it’s not working and I’m not programmer enough to fix it. :-( I'm thinking my syntax is off but my code could be off too since I am not a JavaScript guy.
Any help would be appreciated and thanks for taking the time to read (and respond) to my question! :-D
If you store the data file as JSON you can use AJAX/XMLHTTPRequest to fetch it, and JSON.parse (available in all modern browsers) to read it.
An easier way perhaps is just to have a script that contains just the data, like:
var picPaths = [
'/images/ad-1.jpg',
'/images/ad-2.jpg',
'/images/ad-3.jpg',
'/images/ad-4.jpg'
];
And then include your scripts in the correct order:
<script type="text/javascript" src="picpaths.js"></script>
<script type="text/javascript" src="ad_script.js"></script>
ad_script.js will be able to access picPaths.
You could have some server-side script generate picpaths.js for you, for instance by looking at the contents of a folder or a database and pulling the ad info from that.

javascript get Value

code :
<script type="text/javascript" src="http://127.0.0.1/Test.js#username=stackoverflow">
</script>
iwant to know ,how to get the username in Test.js
file
Test.js :
var username = ??
///////////// #username=stackoverflow
thanks advance
If you are trying to do all this on the client side, it's much better to use:
<script type="text/javascript">//<![CDATA[
var username = "stackoverflow";
//]]></script>
<script type="text/javascript" src="http://127.0.0.1/Test.js"></script>
That way, you don't need to tackle the issue of reading the src attribute of the script tag somehow.
The query portion of the URL is invalid. It should be:
http://127.0.0.1/Test.js?username=stackoverflow
The # is treated as a named anchor.
The gup function isn't good because the parameter is on the script tag, not the HTML output page.
The location object (location.href, location.search...) refers to the HTML page where the script included.
There are 2 other options:
Use this
Use #idealmachine answer. You can wrap the global variable with simple object in order to avoid conflict with other global JS variables

javascript file doesnot load in smarty

{literal}
<SCRIPT LANGUAGE="JavaScript"
SRC="../calendar/weeklycalendar.js">
</script>
<script>
// call the function to build the calendar
// function's param specify the first day of week
// 0=Sunday, 1 = Monday, ..., 6=Saturday
alert("before");
buildWeeklyCalendar(1);
alert("afetr");
</script>
{/literal}
this script runs fine on server but when i use it in smarty template it doesn't work.
Can anyone explain.
Thanks
I figured it out. The js file will be called with reference to the PHP file that calls the template . The path will not be according to where the smart y template is but according to where the calling php file is.
Thanks

Categories