How to run specific code javascript DOM in page at site [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I want help please :(
I have several dom codes, in a js file, running on a site
site.com/script.js
I want to customize a specific code, to work only on a sub-page in the site.
for example:
the codes work on site.com
and I want a specific code that works
only on the site.com/page16
and code javascript just run by one file
I dont have any access to html page

You can access the end part of your url by using window.location.pathname.
For the website site.com/page16 - window.location.pathname returns the string "/page16".
Next you could have a function that checks to see what the current pathname is and terminates early if the pathname doesn't match /page16.
For example:
const subPageFn = () => {
// terminates early if subpage doesn't match page16
if (window.location.pathname !== '/page16') {
return
}
// runs page16 specific code here...
}
Then you'd call the function in any page by using
subPageFn()

Related

How to call php script using jquery and terminate jquery script? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to execute a PHP script using jQuery and terminate jQuery at that point itself. I want the PHP script to be the last page on my browser rather than the jQuery page. I looked it up but every answer included the variable 'data' in it which I don't need. I'm not fetching data from PHP. I want to directly jump from jQuery to PHP and then end the program on the PHP file itself (I don't want to go back to the jQuery page to return some value from the PHP program). How can I do this?
I assume what you want to do is switch to the page ending .php instead of carrying out an ajax request to it and using the result. That doesn't require jquery and is simply done by setting globalThis.location or its href property like one of the following:
location = 'adjacent_file.php'
location.href = '/from/root/distant_file.php'
window.location.href = 'https://some.site/path/specific_file.php'
window.location = '... like the others ...'
Which of the above you pick is up to your needs and variable scope, but setting window.location will probably do you fine.
If you want to make a POST request that lands on the target page, there are a number of good answers already available for this other question - but you will need to create a form through vanilla JS or jquery and then submit it against the target page.

execute dynamically created code in javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In javascript I create a certain command:
arrSetContentCommands.push( "$('#titel"+i+"').trumbowyg('html', \""+data[i].value+"\");";`)
This enables me to use a html editor in browser. The id's it needs to use are also created, cause it depends on what's in the database.
how do i execute this? all the commands are placed in an array.
The solution is: Don't create code strings and put them in an array. Just execute the code:
$('#titel'+i).trumbowyg('html', data[i].value);
If those elements don't exist yet, just run this code later when they do, or if for some reason you won't have access to data later, build an array of functions:
arrSetContentCommands.push(function(index, value) {
$('#titel'+index).trumbowyg('html', value);
}(i, data[i].value));
...and later:
arrSetContentCommands.forEach(function(command) {
command();
});
Notice in that we don't use i or data[i] in the function we're pushing; see this question's answers for why not.
If you had a good reason for executing code in a string, you'd do it via new Function(...)(), like this:
arrSetContentCommands.forEach(function(command) {
new Function(command)();
});
But there are very, very, very few cases where that's appropriate, and obviously never do it with code you don't trust.

PhantomJS only output part of the HTML [duplicate]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have 3 different files in my project and the layout is
phantomjs
-->phantomjs.js
-->phantomjs.exe
index.php
index.php:
$phantom_script = dirname(__FILE__). '\phantomjs\phantomjs.js';
$response = exec ('\phantomjs\phantomjs.exe' . $phantom_script);
echo $response;
phantomjs\phantomjs.js
var webPage = require('webpage');
var page = webPage.create();
page.open('http://www.google.com', function(status) {
console.log(page.content);
phantom.exit();
});
your usage oh phantomjs is correct according to the documentation.
http://phantomjs.org/api/webpage/property/content.html
php exec method returns the last line only.
Maybe that line is a white space.
http://php.net/manual/fr/function.exec.php
You shall have a seond parameter &$output, sent by reference. It is an array containing the entire output.
A problem you may face later, the content could need be to evaluated before you try to read it s DOM document content. Using for example innerHTML of HTML tag, ie: $('html').html();
If the page does not have jquery, you may include it, see this example, https://github.com/ariya/phantomjs/blob/master/examples/phantomwebintro.js
Note also that google may actively desire to not let users scrap and save their search results. Not sure about that.

How to get access to cross domain content? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Two webpages, http://website.com/home.php and another one is on my dropbox http://dropbox.com/myId/public/myfile.html.
I want to run a user script (which will be given out to other people) on home.php with some values, basically some 9-10 numbers. These values are already defined in the script. But they have to get updated on a regular interval, this is done by the user clicking a link or something. These updated values will be on myfile.html. I have full control over this file.
I have tried using localStorage in the script but that doesn't allow to share content over different domains. iframe also doesn't work due to cross domain. But is there a way that these values can be released by a function() on need.html when the iframe loads on home.php?
I'm pretty new to javascript, so I don't know everything.
What you need is JSONP. Here is more on that
I found out how to do it:
I created a script element using the userscript in home.php which links to need.js instead of need.html.
need.js contains code to add the "values" to localStorage.

Types of output that will work for a callback [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am currently working on a small nodejs project that is responsible for returning search terms from a twitter feed. I have the search working however because I am new to this I am having problems displaying this on my webpage. The search information I want to display is being held by a callback from my function and I want to output this information. Keep in mind this information is ascertained after the webpage is loaded and I think that is why response.write(information); is not working for me. However if I do console.log(information); I do get the information I want but it's just not where I want it. Can someone help me out with outputting please.
I ended up solving this by using JSON.stringify(information);
You are right, it's very likely that once you have returned your webpage the response will be closed.
2 options:
1) you load your webpage first on 1 static route (using static middleware) and this web page then does an ajax call to a second route which returns your information.
2) you don't return your webpage until you have retrieved your information. I'm assuming your information will need to be displayed on this webpage which means you'll have to turn your webpage in a template and merge it with your information. You can use a Jade template or underscore template for example.

Categories

Resources