简体   繁体   中英

Hide Url from <a>tag on View Page Source

I am developing a website, where in i have a href tag somewhat like

<a href="Folder1/Sample.pdf" target="_blank">xam study papers</a>

which will open the pdf in a new tab.

Now when i open this website on google chrome and

Right Click->View Page Source

. I can see the same Content .

I want to hide the href link so i tried with javacript

<a href="#" id="id1" >xam study papers</a>
<script>
$( document ).ready(function() {
$("#id1").on("click", function () {             
   window.open('Folder1/Sample.pdf','_blank');
                });
 });
</script>

Still its showing .

So i need to hide the url . What are the best possible methods to do the same. Any help appreciated.

You cannot hide the tags from the source, because the browser require the tags for populating the website.

  • Use Javscript encryption.
  • Disable Right Click , If possible. But Cross platform issues need to be taken care, i would prefer Javascript.
  • Learn on HTML encryption & javascript encryption
  • There is a nice article on this: How to hide your Source Code

How to encrypt HTML source code?

这些问题已在堆栈中得到解答,请检查以下链接https://stackoverflow.com/a/42952848/7751463

You could generate an Id that is paired with the URL, and send the client this unique Id. When the client makes a request with that Id to the server, you know the URL paired with that Id, then you can serve the page with that URL.

In Node, you can do that like:

'use strict';

var express = require('express');
var app = express();
var linkDict = [];

app.get('/', function (req, res) {
    var id = Date.now();
    linkDict[id] = 'mySecretFile.pdf';
    res.send('<html><head></head><body><a href="' + id + '">Secret File</a></body></html>');
});

app.get('/*', function (req, res) {
    console.log(req);
    var id = req.params[0];
    res.sendFile(__dirname + '/' + linkDict[id]);
})

app.listen(3000, function () {
    console.log('Listening on 3000');
})

You can hide a URL from a user, but only truely via PHP. The issue with HTML, is that the browser is still storing that information (you can dress it up with encryption, but, unltimately, you want whoever it is to read it, so they have to know how to decrypt it). Honestly, please just use php tokens for this. Some people even use entire tables in MYSQL, but for what you're doing, I think this will do.

I start by setting the header to application/pdf, this tells the browser to read the byte data as PDF and not HTML nor text. Next, I echo my hidden url's contents.

<?php

if (!empty($_GET['token'])) {
    switch ($_GET['token']) {
        case "1":
            header("Content-type: application/pdf");
            echo file_get_contents('test.pdf');
            break;
    }
    die();
}

?>
<a href="?token=1">xam study papers</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM