Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation since 08/22/2021 in all areas

  1. Let's set a few things up first. Firstly, in HTML create the image tag with an id and some alt text: <img id="image" src="image.png" alt="image-text"> Next, in JavaScript let's assign the image tag to a variable. Doing so will make it's alt property accessible for later use in the div tag: // assign the img tag by id to the "img" variable let img = document.getElementById("image"); Now let's create an empty div in HTML, with an id of "alt-text". You can do this entirely in JS, but let's use HTML for this example. Add it just below your img tag from the first step: <div id="alt-text"></div> Next, in JavaScript let's create a variable to reference the empty div and a function to set the text and background colour of the div: imgFunc = () => { // assign the empty div to a variable let altDiv = document.getElemenetById("alt-text"); // set the text of the div to the alt property from the img variable created earlier altDiv.innerHtml = img.alt; // set the background colour of the div altDiv.style.backgroundColor = "blue"; } Now, there are better ways to set the text, but innerHtml is a quick way to demonstrate. Explore the options available to you on the W3Schools website. I've gone with blue here as the background colour, but you can choose whatever colour you want. Finally, let's add a mouseover event to the img tag from the first step, and use it to call our function: <img id="image" src="image.png" alt="image-text" onmouseover="imgFunc()"> And that's it.
    1 point
  2. You cannot return the data, you have to do something with it. Print it, manipulate it, store it into a variable or something else. In my previous example, I used console.log() to write the data to the Javascript console. Javascript is not allowed to write to files, neither on the server nor on the user's computer, but you can prompt the user to download a generated text file like this: <script> function file_get_contents(filename, callback) { fetch(filename).then((resp) => resp.text()).then(callback); } function handleResponse(data) { // This code generates a text file with the data and asks the user to download it var blob = new Blob([data], { type: "text/plain;charset=utf-8" }); var a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = "file.txt"; document.body.appendChild(a); a.click(); setTimeout(function() { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); } file_get_contents('https://mediainfo.tf1.fr/mediainfocombo/L_LCI?context=MYTF1&pver=4014002', handleResponse); </script>
    1 point
  3. The code will need to be updated so that you are handling the data in the callback function. It actually cannot work identical to PHP's function because it's asynchronous. This would work: <script> function file_get_contents(filename, callback) { fetch(filename).then((resp) => resp.text()).then(callback); } function handleResponse(data) { // Do something with the data here console.log(data); } file_get_contents('https://mediainfo.tf1.fr/mediainfocombo/L_LCI?context=MYTF1&pver=4014002', handleResponse); </script>
    1 point
  4. Just running that code in my own HTML document does not result in any errors. You shouldn't include the jQuery library twice. Either load it from the CDN or from your own server, but not both places. I would recommend loading it from the CDN for now to avoid errors with file paths.
    1 point
  5. Looking at the HTTP headers, they are not permitting cross-origin requests on that file.
    1 point
  6. Without seeing any of your code it is hard to know what is causing the problem. This could happen if your <script> tag occurs before the <script> tag which loads the jQuery library.
    1 point
  7. Not from other websites. Browsers implement this restriction for security purposes. Applications that want to work around the restriction will have a back-end script which fetches content from other websites and sends it to the front end.
    1 point
  8. As with all AJAX-style APIs, with fetch() you're only permitted to load files from your own server unless the target server has granted permission to your website.
    1 point
  9. Inline elements like <strong> do not handle margins and padding in the normal way. Vertical margin and padding are ignored. You have to set the display to inline-block to get normal behavior.
    1 point
  10. If the page is blank it means that your server has disabled error reporting. You can turn error reporting on for your page by putting these lines at the very beginning of the file: <?php ini_set('display_errors', 1); error_reporting(E_ALL); Error messages will help pinpoint exactly where the problem is and likely indicate how to fix it.
    1 point
  11. Did you get any error messages? It's not obvious where the problem might be but there are a few potential problems in the code. The first thing that I notice is that the session_start() and header() are being called after HTML has been sent to the client due to the fact that the file which contains this code is included after some HTML in another file. session_start() and header() need to be called before and output, which could be HTML or any echo or print statements, among other things. The way to solve the above problem is to separate the code which generates HTML from the code which processes the form data. The code which processes the form should be included at the very beginning of pro.php. The other include statement embedded within the HTML could use variables created by the code at the top of the page to display information. The following line of code won't display an image because the src attribute is just "pro.php" which is not an image file: <img class="image-preview" src="<?php echo 'pro.php'; ?>" class="upload-preview" /> I think what you intended was to display the path of the uploaded image, which would be like this: <img class="image-preview" src="<?php echo $targetPath; ?>" class="upload-preview" /> Your code has some dangerous security holes. It allows for the possibility of hacking the database. Read about Prepared Statements to protect your website from that. Another security vulnerability is that people are able to upload their own PHP file to your server and run any PHP code they want. A common strategy to protect from this is to make sure that the file extension only is one of png, gif, jpg or jpeg. W3Schools shows an example of this on this page: https://www.w3schools.com/php/php_file_upload.asp To add an additional layer of security to the file upload form you could verify that the contents of the file is an image by using getimagesize().
    1 point
  12. https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_draggable <script> //Make the DIV element draggagle: dragElement(document.getElementById("mydiv")); It should be "draggable", but instead it's "draggagle".
    0 points
×
×
  • Create New...