最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hide Url from <a>tag on View Page Source - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Mar 10, 2017 at 7:30 Shreyas Achar asked Mar 10, 2017 at 7:13 Shreyas AcharShreyas Achar 1,4356 gold badges36 silver badges64 bronze badges 4
  • I have corrected it – Shreyas Achar Commented Mar 10, 2017 at 7:30
  • Place your script in a separate .js file and include it as a link in your HTML, and it will not show in view source. Only if the person acctually opens the specific file from developer menu. – Arg0n Commented Mar 10, 2017 at 7:32
  • please use the below example which is already answered – Dilip D Commented Mar 23, 2017 at 12:19
  • stackoverflow./a/42952848/7751463 – Dilip D Commented Mar 23, 2017 at 12:19
Add a ment  | 

4 Answers 4

Reset to default 2

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?

These question is already answered in stack please check the below link https://stackoverflow./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>
发布评论

评论列表(0)

  1. 暂无评论