I have an audioplayer running on my website.
Currently there's no problem with the player although I wish to obfuscate my javascript code.
The problem is that the javascript is called on a page which includes a php file.
the player.php contains:
<script type="text/javascript">
$(function(){
$('.player').plate({
playlist: [
<?php
include ('inc/trackimport.inc.php');
?>
]
});
});
</script>
And trackimport generates (in a foreach for each $dj found in the database):
echo '{"title":"'.$dj.'", "artist":"'.$set.'", "cover":"http://domain.com/'.$imglink.'", "file":"http://domain.com/'.$djurl.'/Archive/'.$url.'"},';
Is there away to make this full code obfuscated?
Your best bet would be to use one of these PHP-based JavaScript minifiers. It's basically the same effect, but you can probably put in a configuration flag to make one of them run only when you want it.
Related
I currently call a file using onload in javascript.
It works fine, I just can't seem to get it to work in Laravel, if I load an html file it's fine, but not a php file.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#query").load("test.php");
});
</script>
</head>
<div id="query"><img src='https://m.popkey.co/163fce/Llgbv_s-200x150.gif'/></div>
<div id="query2"><img src='https://m.popkey.co/163fce/Llgbv_s-200x150.gif'/></div>
Results are blank.
The test.php file is located in the public folder, it just has a simple echo in it:
<?php echo 'This is Working';
I have a test.html in the same folder that returns what ever I put in it perfectly, it seems to be an issue with javascript pulling the php file.
If i run this url : https://domain/Home/public/test.html it works fine, if i run this url https://Domain/Home/public/test.php it also works fine. The test.php echo's out This is Working
I had a similar issue a while back.
What i did was not run the file directly from the public folder, i created a Route.
Route::get('test.php', function(){
echo 'This is Working';
);
Now you can either run what you want directly from this function or return it to your view.
Now you can use :
<script type="text/javascript">
$(function() {
$("#query").load('test.php');
});
</script>
What Configuration are you using? This link shows that it's a problem using Laravel Valet. Github Issue Here.
I would recommend that you use an API route for loading these images and try to keep everything within the Framework for the sake of neat and tidiness?
But for a quick solution:
Route::get('foo.php', function () {
return 'foo';
});
API: (Within routes/api.php
Route::get('/test', function () {
return asset('assets/images/image.jpg');
});
and your JS would point to your URL of the Route.
Add the script tag of jQuery function in body and make sure you have used jQuery CDN for access the jQuery function
Can you please try this?
$("#query").load("{{ asset('public/test.php') }}");
I think this solve your problem
Is there a PHP require_once or include_once for <script>? I know <script> is pure HTML, but does PHP or HTML have such a thing?
I would like to prevent javascript from loading twice in the same page.
Example:
<script type="text/javascript" src="js/jquery-0.4.ajaxify.min.js"></script>
Seems like you are looking for this:
http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload
One of the way you can run script tags in php would be
<?php
....Here is php code
?>
... add the script here(<script>....</script>
<?php
?>
Another probable way is :
Using php inside the script tags for example:
<script type="text/javascript">
var alertMsg = '<?php echo $custom_message; ?>';
alert(alertMsg);
</script>
If you are using buttons
echo('<button type="button" onclick="customfunction();">My Button</button>');
<script>
//call your customfunction here
</script>
AN UPDATE TO SOLVE LOADING SCRIPT CONTENTS TWICE
I would suggest use of javascript function which are called to the specific page as they are needed an example would be
Create a file with a .js eg
example.js
here declare your functions you would put in the script tags of a html page
In the page you want to use the <script>
Include the example.js file and you can call the custom functions from there use the obect orientend approach
Check This resource for more info about obect orientend javascrip
I would like to load the following page inside another page after the main page has loaded. I have the following code:
<script language="javascript">
$(document).ready(function(){
$.get('http://$_SERVER[SERVER_NAME]/X.php?logon=Y&vendorref="<?php .$row_RS_Product['id'].?>"&mode=product', function(data) {
$('#tabs-6').html(data);
});
});
</script>
I am not very familiar with this code so please excuse me if I made the obvious mistake
Could anybody help please as it does work when using file_get_contents in php, but this is slowing the page load down tremendous.
Any help welcome
I think you want to get $_SERVER[SERVER_NAME] from PHP so you need to add <?php echo when creating the url to get from. Also you need to have echo when opening the php tag to echo, not . .
Dot is used to concat string in php like this "aaa" . "bbb" (without opening the php tag)
So you need something like this:
$.get('http://<?php echo $_SERVER[SERVER_NAME]; ?>/X.php?logon=Y&vendorref="<?php echo $row_RS_Product['id']; ?>"&mode=product', function(data) {
$('#tabs-6').html(data);
});
I am trying to place a javascript ad zone inside a php function. I am doing this to control what ad zones are placed on each page. Currently in the php template I am using:
<?php
if(is_page('welcome-president')) {
oiopub_banner_zone(9);
oiopub_banner_zone(19);
}
?>
I am trying to place this javascript code inside the if conditional tag instead of the oiopub_banner_zone(9); so that the ads will not be cached and rotate.
<script type="text/javascript" src="http://rutgers.myuvn.com/wp-content/plugins/oiopub-direct/js.php#type=banner&align=center&zone=9"></script>
Thanks in advance!
Rather than call the function oiopub_banner_zone(9) to display the banner code, you can just replace that function call with echo and the actual script tag that you would like to output on the page.
<?php
if(is_page('welcome-president')) {
echo '<script type="text/javascript" src="http://rutgers.myuvn.com/wp-content/plugins/oiopub-direct/js.php#type=banner&align=center&zone=9"></script>';
oiopub_banner_zone(19);
}
?>
If you want to prevent caching. Make an that points to some place in your site ( e.g. /ad_iframe.php ) and do the rotate logic to retrieve the add content there. That will not get cached.
There was a post about it that helped me a lot.
Preventing iframe caching in browser
Good luck, and cheers.
Is there any way to load some text from another file into javascript, without server side code?
I was thinking to use another element to hold the text inside some comments, but I don't know how to read it's source code with javascript.
Something like:
<script src="myfile.js"></script>
<script> function readMyText() { ... }</script>
In myfile.js:
/* some text */
You can put anything you want into a script tag if you give it a "type" that's not something the browser understands as meaning "JavaScript":
<script id='Turtle' type='text/poem'>
Turtle, turtle, on the ground;
Pink and shiny - turn around.
</script>
You can get the contents via the "innerHTML" property:
var poemScript = document.getElementById('Turtle');
var poem = poemScript.innerHTML;
Here is a jsfiddle to demonstrate.
That trick is popular lately with people doing client-side page building via templates.
Without using ajax or any server code... sorry mate but you can't :(
Building on Pointy's answer, to import from local files do this:
<script src="foo.txt" id="text" type="text">
</script>
You could also use this to target an external file:
<script src="http://foo.txt"></script>