.innerHTML from .js file? - javascript

i want to print all text from .js file to div
document.getElementById('cart-status').innerHTML = "/numberOfcart.js"

You could use jQuery to do something like this:
$.get('/numberOfcart.js', function(data) {
$('#cart-status').text(data);
}
but you are simply loading in the .js file, not running it.

Directly displaying the the contents of js file by JavaScript is not possible (or I don`t know, if somebody knows a way - welcome). So by PHP:
$file = file_get_contents( 'script.js' );
echo "<pre>" . htmlentities ( $file ) . "</pre>";
With an ajax callback to the php script, set to the div.

Related

Modify a local JS-file using php and javascript

Information
For testing purposes I am building a system where a javascript file contains an array called "contains".
The javascript will then make the browser redirect to another page where the php-code is located, and that code should add the keyword as a new array-element
And before anyone of you try to say how simple this is, let me reiterate that I am injecting the JS code on another page using Tampermonkey, and I have no chance to modify their code
The code should then be converted to JS again and be written to a file which is called "copyright.js"
Code
At the moment the "copyright.js" file looks like this
var contains = ["original"]
The following code returns a positive result
$str = 'var contains = ["original"]';
$str = str_replace("var ", '$', $str);
eval($str.";");
But when I try to fetch the variable from the file i get an error
$file = fopen('copyright.js', 'r') or die('Unable to open File');
$str = fread($file, filesize('copyright.js'));
fclose($file);
$str = str_replace("var ", '$', $str);
eval($str.";");
If there is a better solution to this, please don't hesitate to tell me :)

js: how to hide/crypt the src of the image?

I am looking for a way to hide/crypt the image src.
It is a quiz game and image name contains the solution
ex: <img src="solution.jpg">
Solution would be to encode64 the image, but this is quite heavy solution.
Suggestion does not have be to 100%secure, just avoid showing clearly the "solution.jpg" src
Best solution is to store the file path in database and serve it on request. An example using php would be
HTML
<img src="get-image.php?id=2653" />
PHP
// get image path from database
...
// output
header("Content-type: image/jpeg") // change format accordingly
header('Content-Length: ' . filesize($filepath));
ob_clean();
flush();
readfile($filepath);
die();
I know it's not php question but you can create php script that will read the file based on md5 hash. On the beginning of your main script you save hash in session:
session_start()
$time = array_sum(explode(' ', microtime()));
$_SESSION['hash'] = md5($time);
and in php script that you use for image
<img src="script.php?hash=<MD5 HASH>"/>
you check if $_GET['hash'] is equal to value from session:
session_start();
if (isset($_GET['hash']) && isset($_SESSION['hash']) &&
$_GET['hash'] == $_SESSION['hash']) {
header("Content-type: image/jpeg");
echo file_get_contents('your hidden image.jpg');
}

Render .mht files dynamically without load/iframe/embed

I have .mht files that are stored on a folder on my server. This folder has a rule in .htaccess that only localhost is allowed inside.
I need to render files on my website depending on user actions.
<iframe> and <embed> and jquery .load() won't work as the request doesn't come from localhost.
I'm trying to get the data of the file via a phpscript and an ajax call :
PHP:
$file = htmlentities(filter_var($_GET['url'], FILTER_SANITIZE_STRING), ENT_QUOTES);
$content = file_get_contents(".".$file);
$return = array("content" => $content);
echo json_encode($return);
jQuery:
$.getJSON('queries.php',{q: 'getFile', url: file},
function(data){
$('#file_panel').html(data.content);
}
);
But it only displays the content of the file, it doesn't render it. Any suggestion to get it rendered?
It will not be possible unless you use an <iframe>. Browsers won't parse HTML & MHT within the same page.
Try this,
PHP File
$file = htmlentities(filter_var($_GET['url'], FILTER_SANITIZE_STRING), ENT_QUOTES);
echo file_get_contents(".".$file);
JS
$("#frame").attr("src", "queries.php?url="+file);

Downloading files using Header with a JS input

I have a page that has a JavaScript function that uses Post to send a variable to a php file. The problem is, that I am using "header" to download the file and my JS does not open the PHP script in a new page.
When I open the php file in a new page, it does not receive the needed variable from the JS.
I know it sounds confusing, but I hope my code can shed some light on my problem.
The short version is, I am trying to download a file that is selected by a radiobutton. I use JS to check which radiobutton is checked and then send that to my php file. Which then needs to download the file.
Thank you all in advance.
PHP:
<?php
if (isset($_POST['routenumber'])) {
if(!isset($_SESSION)){session_start();}
$routenumber = (isset($_POST['routenumber']) ? $_POST['routenumber'] : null);
$directory = ("Users/".$_SESSION['id']."/SavedRoutes/");
$routes = scandir($directory);
sort($routes);
$route = $routes[$routenumber];
$file =("Users/".$_SESSION['id']."/SavedRoutes/".$route);
header("Content-type: application/gpx+xml");
// header("Content-Disposition: attachment;Filename=".json_encode($route).".gpx");
header("Content-Disposition: attachment;Filename=route.gpx");
readfile($file);
}
?>
JS:
function fuAccountDownloadRoute(){
var i=2;
var SelectedRadio
while (i < routecounter){
var str1='radio';
var str2=JSON.stringify(i);
var result = str1.concat(str2);
if (document.getElementById(result).checked){
SelectedRadio = result.slice(5);
}
i=i+1;
}
$.post('accountPage.php',{routenumber:SelectedRadio});
}
When you open the url: http://localhost/accountPage.php in your browser it makes a GET request. You should change all the $_POST to $_GET in your code if you want to make it possible, and then you can open it like this: http://localhost/accountPage.php?routenumber=3, though it's probably not what you really want.

php create file in directory

The below code checks for a directory 'dat'; if it ins't there, it creates one. That part works just fine; what I need is for it to write a file to said directory where AJAX can read it from.
Here's the php...
//checks for 'dat' directory; if false, creates it, if true, does nothing.
$dir = 'c:\wamp\www\dat';
if(file_exists($dir)){
return;
}
else{
mkdir ('C:\wamp\www\dat',0700);
}
//writes chats to file
$data = fopen($dir. "/chatlog". date('d'). '.txt', 'a+');
fwrite($data, $speak);
fclose($data);
}
And here's the AJAX; I don't need as much help here as I do above, but I won't complain if you provide the help for the AJAX below, mainly in getting it to read from the file within the 'dat' directory...
xhr.open("GET","chatlog<?php /*stamps the chatlog file with date (numerical day only)*/ echo date("d");?>.txt",true);
Your PHP script is running inside www, then, your file you be created there.
If you want to create the file inside the directory www/dat, just change this line
$file = "chatlog". date('d'). ".txt";
for this one
$file = 'dat\chatlog'. date('d'). '.txt';

Categories