Access Get Directly from JavaScript

How to Detect direct access to html page

If you mean if it's a user, check for the X-Requested-By header in the document's request.

If you mean if it's a direct URL typed in, check for the Referrer header.

Can I access DOM element directly by id?

Yes, in some browsers, the elements are available as global variables named for their ID.

Don't use this "feature". It's non-standard, and not universally supported.

How to call or access a class method in JavaScript from the browser's console?

There are multiple possible approaches here. One of them could be to store the newGuitar instance in some global variable and then access it in your console.

So try adding this to the end of your script.js file:

window.newGuitar = newGuitar

Then in your browser console you can access the method like window.newGuitar.changeDetails()

NOTE: storing instances in global variables is not a good practice and it should be used just for some debug reasons etc…

How to access client-side javascript variable from external page?

There are different approaches to get the variable, depending how they are created.

  • variable is directly in source code so you can simply parse it using regEx for example.
  • variable is being evaluated in JS runtime, in this case you need to mock the browser environment using PhantomJS, which is quite heavy.

block direct access to file but allow access through jquerys load function

Please follow below steps to achieve:

  1. In the .load function of jquery post a security code.
  2. In the Feed.php page place a PHP condition if the posted security_code params found and match with security_code passed in the .load then only allow to access the page otherwise restrict.

Please follow below changes in your existing code to achieve it.

JS

<?php 
$_SESSION['security_code'] = randomCode();
?>
<script type="text/javascript">
$("#dock-left-container").load("feed.php", {
security_code: '<?= $_SESSION['security_code']; ?>'
}); // load feed.php into the dock-left-container div
</script>

PHP

Place php condition in the top of feed.php

if(isset($_POST['security_code']) && $_POST['security_code'] == $_SESSION['security_code']){
//Feed.php page's all the stuff will go here
}else{
echo "No direct access of this page will be allowed.";
}

Detect if link was used to access page

This is called the Referrer:

https://developer.mozilla.org/en-US/docs/Web/API/document.referrer

In your javascript:

var myReferrer = document.referrer;

The referrer property is supplied by the user's browser, like the userAgent.

Demo

The referrer value tells you the link used to access the site, so to merely detect it:

var hasReferrer = document.referrer != "";

The demo should show that now - if you follow the link above you'll get the StackOverflow id and hasReferrer will be true; if you click into the nav box and hit enter (i.e. direct navigation), you should get false.

hasReferrer here is a boolean value, so it holds either true or false. To use in an if-statement:

if(hasReferrer) {
// came from a link
} else {
// direct navigation
}

How to directly access (javascript) split values?

It's incredibly inefficient, but you could call split multiple times:

var iwilluseit = 'something' + content.split('|')[1] + content.split('|')[2];

There's also the slice() + join() option:

var iwilluseit = 'something' + content.split('|').slice(1,2).join('');

Really, though, just creating the temp variable is the best way to go.

How do you access the unaltered source of a page via phantomjs

There is no way to directly access the unaltered source (referred to as view-source in other browsers) in PhantomJS.

You could try to read the page from the PhantomJS cache (when run with the --disk-cache=true option), but there is an easier method. You can simply sent an AJAX request to get the source "on the wire", but then you would need to handle redirect yourself.

var page = require('webpage').create(),
fs = require('fs');

function get(page, url) {
return page.evaluate(function(url){
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send(null);
return xhr.responseText;
}, url);
}

var url = 'http://example.com';

page.open(url, function(){
var co = get(page, url);
fs.write("original.html", co);
fs.write("rendered.html", page.content);
phantom.exit();
});

You can already see with this simple script that the two files are different despite not involving JavaScript.

Sample Image

You might need to run with the --web-security=false option. Instead of passing the url into the get() function, you may directly access page.url:

function get(page, url) {
url = url || page.url;
return page.evaluate(function(url){
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send(null);
return xhr.responseText;
}, url);
}


Related Topics



Leave a reply



Submit