PhP and Js writing problems in html file - javascript

so here is my problem :
There is my index.html and I want to write on it with php and javascript in order to check that the text is correct. When I start the index.html from one of my computer the javascript text displays correctly but not the php.
So I create a .htaccess and launch the index.html again using mylocal server then the php text display correctly but the javascript isn't anymore (using write or alert)
So what do I have to do in order to be able to use both ?
here is the code :
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Test</title>
<link href="style.css" rel="stylesheet"/>
</head>
<body>
<?php include 'parsing.php';?>
<script>
write("write");
alert("alert");
</script>
</body>
</html>
Thanks for your help.

the parsing.php is as simple as that :
yes, I have a webserver with the php plugin that's why it works in the second case only.
I am testing the page with this page : http://localhost/index.html
Thanks for your answers I think it was caused by my company proxy or something like that (their computer are full of problems anyway...)
I retried afterwards without changing anything and now it works.
Thanks for your time.

Related

JavaScript separate file in script src tag detecting but not working

I am learning JS basics and trying to separate my js code from html file.
Looks like IDEA detects .js file correctly, but after launching - alert doesn't pop up.
If alert() is inside the script tag (without separate .js file) - everything works fine.
This is an educational project based on Java and TomCat server, so maybe the problem is there.
Can someone help?
My HTML file:
<!doctype html>
<html lang="en">
<head>
<title>RPG</title>
<link rel="stylesheet" href="/css/my.css">
</head>
<body>
<h1>RPG admin panel</h1>
<script src="app.js"> </script>
</body>
</html>
app.js file:
alert(1);
Screenshot of project hierarchy:
hierarcgy
Idea file detection:
file detected
UPD:
Try 1
differnet source
UPD 2
Found an error. Still no solution.
error msg
detailed error

Is there a way to call another .html file in my main html or in javascript

I was trying this find this out but too no avail. I was just wondering if you could call another separate html file in your main html.
I know you can do it for javascript
<script src = "Classroom_names.js"></script>
So I was wondering if you could do it also for it in html. For example
<script src = "field_names.html"></script>
Or if you could not do it like that would you be able to call a html file in javascript andthen set a condition to it.
Thank you for taking your time to read this guys
The easiest way to do it is using jQuery, which you must include in your project, and then use the load function to place it in some node of your website, in this case I used a div with the class "menuContainer" and the menu I have put in a file called menu.html
Important: You must have a web server running on localhost to be able to use this function since if you open the file using the url "file: /// your_file.html" through the browser it will give you a cross-origin error, if you don't have a server web running you can mount it very easily with python3 using the http.server module or in nodejs using http-server.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="menuContainer"></div>
</body>
<script>
$(document).ready(function () {
$('.menuContainer').load('./menu.html');
});
</script>
</html>

Web Development - Security

In terms of security, is it generally a better practice to keep all of your javascript files embedded in the source html, or is it more secure to link to an external html file from the source html. Are they treated the exact same and I'm just being an idiot?
I am very new to web development and I don't really understand the encryption side of things:
(1) Embedded javascript
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
...
<script>
function myFunction()
{
// is this secure?
}
</script>
...
</body>
</html>
(2) External javascript
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
...
<script src="myFunction.js"></script> // or is this better?
</body>
</html>
Anything that makes it to the client side can be captured and viewed. So all JS files are open to viewed along with your HTML.
For security, if you have business logic or sensitive code that needs to remain unseen you need to place that on the server side and only access the results of that code running via API calls.

How to import codes in .html file which are placed in a txt file ?

So, what i want is this. I have an HTML File and i want the code to load from a text file. So, the browser should get the code from that text file and read it as a part of the HTML code. Here is an example:
Suppose I have an HTML File whose code is :-
<html>
<head></head>
<body>
<div src="code.txt"></div> [ I made this code. I know this code does not exist.]
</body>
</html>
And i have text file named 'code.text' :
<h2>Welcome</h2>
<img src="sample.jpg">
<p>This is the paragraph.</p>
So, instead of writing code in the actual HTML File, i saved the codes in a text file and want to use them whenever i want. I know the following code does not exist.
<div src="code.text"></div>
I made up this code just to show what i want.
**
Just tell me the way i can achieve this purpose.
**
Thanks,
Waiting for your answer !
You can load the content of this with jQuery ajax. Your div will look like this: <div id="content"></div>
And you call the document using ajax:
$.ajax("code.text").done( html =>{ $('#content').html(html) });
You can view more on http://api.jquery.com/jquery.ajax/
Why not use SHTML?
SHTML is an HTML file that includes server instructions or server side includes and is similar to an ASP file.
It's embedded in a standard XML comment, and looks like this:
<!--#include virtual="../top.shtml" -->
<!--#include virtual="../quote.txt" -->
For example, a .shtml page that includes a resource can be as follows:
<!doctype html>
<html lang="en">
<head>
<title>My SHTML Page</title>
</head>
<body>
<!--#include virtual="content.html" -->
</body>
</html>
You can use to include a common header and footer in your pages, so you don't have to repeat code as much, and changing one included file updates all of your pages at once.
Apache can be configured to parse any file with a particular file extension, such as .shtml, with the following directives in the http.conf file:
AddType text/html .shtml
AddHandler server-parsed .shtml
I got the SOLUTION.
And the solution is to use "php include".
<?php include("code.text"); ?>
OR similarly you can use it for other files.
<?php include("your_html_code.html"); ?>
<?php include("your_html_css.css"); ?>
You can import any code files like this (text,html,css etc.) and the browser will read the codes as a part of the main code.
This way you can use it to create templates so that you don't have to type the same code again and again.
Also, if you intend to make some changes, then the changes will be applied everywhere. This will save you a lot of time if you are creating a blog or if your website has many pages with some same sections.
Get the full guide for using this technique of php 'include' here:
http://www.apaddedcell.com/how-automatically-include-your-header-navigation-and-footer-every-page

HTML, calling JS (API)

I have few issues with calling chessboard.js (http://chessboardjs.com). I downloaded API and made new HTML file:
<!DOCTYPE html>
<html>
<head>
<title>Super chess/title>
<meta charset="UTF-8"/>
<script src=":\path-to-js-file\jschessboard-0.3.0.js"></script>
</head>
<body bgcolor="lightgrey">
<div id="board" style="width: 400px"></div>
<script>
var board = new ChessBoard('board', 'start');
</script>
</body>
</html>
I tried to draw chessboard. What I'm doing wrong?
Thanks.
From what I can tell based on your code and the docs, you're missing two things:
1) Your title tag isn't closed on line 4 of your html file
2) After you've fixed that problem you'll get an error saying "$ isn't defined" in the chessboard.js file. I teased out that JQuery is a dependency for the chessboard.js file. If you include JQuery in your html (either download the file like you've done with chessboard.js or use a CDN).
You should be good after that!!
Update:
Tried almost everything, here is my local directory:
https://www.dropbox.com/sh/3unwsb8esh9100o/AADHEB8sojQy1PnpLyC8fmSLa?dl=0
I also downloaded jQuery to my local directory, but page still dosen't load. Dev tools in Chrome doesn't import anything.
And also tried with xampp (Apache server), because sometimes code doesn't work if you calling it from local directory.

Categories