开发者

Performing JS methods on a URL submitted by the user?

开发者 https://www.devze.com 2023-02-11 11:19 出处:网络
I haven\'t found an answer to this, and since I\'m pretty new to JS, I don\'t know if it\'s even possible.

I haven't found an answer to this, and since I'm pretty new to JS, I don't know if it's even possible.

I have a regular HTML form, where the only field is a user types in a URL (any URL) and clicks submit.

The URL will "be sent" to JS code that stores this URL in some variable, I guess. Basically, I need to be able to call getElementsByTagName() on any URL submitted by the user.

My point is to count up the number of times a URL contains a specified element, which I do kno开发者_运维百科w how to do :)

How do I interpret a URL submitted through a form by someone and then take that URL and be able to perform methods (such as getElementsById) on it? I want to return the count of the number of elements to the user.

Any ideas? Can this all be done in JS? Is this possible?


When you say "URL," I assume you are talking about the actual webpage and not the url string. In other words, you want to load the entire DOM into a javascript variable and then parse it with getElementsByTagName(), etc. Javascript cannot load this webpage due to the Same Origin Policy, unless users can only submit pages that are on the same domain as your site. If that was the case, you could use a frame. Otherwise, JS can't do it without Jsonp, which isn't going to work in this case.

However, all is not lost. You can have your JS make an asynchronous request (ajax) to your own server. Your server scripting language /can/ get the entire DOM of the webpage (e.g. PHP can do this with cURL). Then it can send the entire string back to JS as xml that can be parsed. Good luck.


You can't really do that from the client (the web browser) with nothing but Javascript, because security rules will prevent your page from fetching and examining content from a different domain. You'll need to send the URL to a server and have it do the work.

0

精彩评论

暂无评论...
验证码 换一张
取 消