Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 354 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JavaScript resolve string to equivalent object

#1
I am writing a JavaScript utility which allows a user to detect if a particular object / function is available at runtime. Here is the source code, this works but it needs editing every time you want to test for another object:

<!DOCTYPE html>
<html>
<head>
<title>HTML5 Template</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#TestObject').click(function() {
alert(typeof(HTMLCollection));
});
});
</script>
</head>
<body>
<input id="ObjectType" type="text" />
<input id="TestObject" type="button" value="Test" />
</body>
</html>

When the button is clicked, it displays an alert indicating "object" or "function" if the item exists, and "undefined" if it does not.

What I want is to have a textbox `<input id="ObjectType" type="text" />` where I can type in the object to test, and then click the button to test it, which will eliminate the need to keep editing the document. Is this possible? Is there anything similar to reflection that I can use?
Reply

#2
You can get the object from a string-ed class name by doing `window[className]`. We'll use that by getting the string value of a class name from the input text box, getting the object from the class name, and calling typeof against it:

Keeping your code the same just replace ` alert(typeof(HTMLCollection));` with ` alert(typeof(window[$("#ObjectType").val()]));`
Reply

#3
This is possible due to JavaScript object properties actually being associative key-value pairs, meaning that `obj.property` is equivalent to `obj['property']`.

Applying this to your problem, the following code would work:

alert(typeof(window[$('#ObjectType').val()]));

This works because all "global" variables, are actually properties of the `window` object.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through