Code Monkey home page Code Monkey logo

weird-js's Introduction

weird-js

Demonstrating some weird features of JavaScript

This demonstrates how a tab opened by a regular HTML link can be closed automatically. The link will be clicked, and then the click can be prevented by using a simple function called preventDefault()

linkElement.addEventListener("click", (e) => {
    e.preventDefault(); // Prevent the link from being clicked
})

and then, opening a new tab (using window.open) and closing it after a while. Users might think this is a link that takes them to the new page but it actually is a new window object made by JavaScript, when we got input from the user, we cancelled it and used it for another purpose.

linkElement.addEventListener("click", (e) => {
    e.preventDefault(); // Prevent the user from clicking the link
    tab = window.open("https://wikipedia.org"); // Open a new tab
    setTimeout(() => tab.close(), 2000); // Close the newly opened tab after 2 seconds
})

Kinda scary but fun at the same time.


This demonstrates how whatever you copy from a website can be modified, when you copy text, preventDefault() is called to prevent copying it, and clipboardData.setData() is used to modify the contents of the clipboard.

element.addEventListener("copy", (e) => {
  e.clipboardData.setData("text/plain", "Foo Bar"); // Use the event to copy something into the users clipboard
  e.preventDefault(); // Cancel the actual event.
})

This is why you should always be careful when you copy text from a website and paste it somewhere else.


JavaScript has access to lots of information regarding your browser and computer, this info is not very sensitive but can be easily used to uniquely identify a user. The navigator object contains a lot of info about the user's browser and computer without requiring the user's permission, here's an example. The most commonly used is navigator.userAgent, which returns the userAgent string of a user, which contains the name and version of the user's Operating System and browser.


weird-js's People

Contributors

chiroyce1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jeffalo wulliy

weird-js's Issues

New

Make something else to copy when we copy a snippet of code lol

It's possible

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.