Question

Fake News This exercise is about modifying the content of a page in your web browser using Javascript. As we have said, Javascript has access to the current page via the Document Object Model or DOM....

Fake News

This exercise is about modifying the content of a page in your web browser using Javascript. As we have said, Javascript has access to the current page via the Document Object Model or DOM. In your browser, the variable document represents the current document and your code can use it to read and write to the current page. In this exercise we will use the developer tools Javascript console to write Javascript to access parts of the main page of a newspaper site and, just for fun, modify their contents. The instructions here relate to Chrome but some variation will work in any browser.

  1. Go to the main page of a news website, eg. SMH, Guardian, Telegraph, Buzzfeed or whatever (it should have headlines on the main page)
  2. Right click on one of the headlines and choose "Inspect"
  3. This should show you the HTML of the page with the element corresponding to your headline highlighted. Take note of the kind of element it is (eg. <h3>) and any class attributes it has. You will use these to select this element in your Javascript code.
  4. Switch to the Console tab in developer tools

The console allows you to run Javascript code in the context of the current page, so the document variable will represent the page you are viewing. Write Javascript code to select all of the headline elements, so if you saw that they were all <h3> elements you might write

headlines = document.getElementsByTagName("h3")

Take the first of these and find the HTML content of the heading:

headlines[0].innerHTML

Now modify this to add your own message to the page

headlines[0].innerHTML = "COMP249 Found to be Best Unit in the Universe!"

Check the page to see your updated headline.

Ok, so we can fake the news, that's good but we can go further.  

  1. Write a for loop that changes the content of all of the headlines to your chosen message
  2. Write a function that takes your new message as an argument and modifies the headlines when it is called

Now, be careful to only use your new-found powers for good, not evil.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here telegraphIndia web site is used for this demonstration.

Screen 1 :Edited heading first h3 .

TT Telegraph India Todays Latest+ N O ← → @https://www.telegraphindacom VPS Spring Sale Sove 5x5 India INDIA COMP249 Found t

a for loop that changes the content of all of the headlines to your chosen message :

headlines = document.getElementsByTagName("h3");

for(i=0;i<headlines.length;i++){headlines[i].innerHTML = "COMP249 Found to be Best Unit in the Universe!";}

Screen 2 :

TT Telegraph India | Todays Latestx+ ← → C https://www.telegraphindia.com India hosting support COMP249 Found to be Best Uni

a function that takes your new message as an argument and modifies the headlines when it is called :

//function defination

function changeHeading(message) {

headlines = document.getElementsByTagName("h3");

for (i = 0; i < headlines.length; i++)

{

headlines[i].innerHTML = message; }

}

changeHeading("myHeading");//function call

Screen 3 :

TT Telegraph India | Todays Latestx+ ← → C https://www.telegraphindia.com India hosting support myHeading LEVEL UP AT LIQUID

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Fake News This exercise is about modifying the content of a page in your web browser using Javascript. As we have said, Javascript has access to the current page via the Document Object Model or DOM....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WEB230 - JavaScript 1 Assignment 6a Use Events and DOM manipulation to create a playable Tic...

    WEB230 - JavaScript 1 Assignment 6a Use Events and DOM manipulation to create a playable Tic Tac Toe game. Do the following using JavaScript. Do not modify the HTML file. When you see this symbol: save and refresh the browser (this should happen automatically if you are using Brackets with Live Preview), then check your code to make sure it is working before you proceed. 1. Create two variables called "playerX" and "playero". Set them to the DOM element with...

  • NEED HELP with HTML with Javascript embedding for form validation project below. I have my code...

    NEED HELP with HTML with Javascript embedding for form validation project below. I have my code below but I'm stuck with validation. If anyone can fix it, I'd really appreciate. ****************************************************************************** CODE: <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Nice</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> var textFromTextArea; function getWords(){ var text =...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT