How to extract javascript variable value using imacros browser? - javascript

I have looked but I simply cannot find an answer to what I expect should be a very simple task.
I have navigated to a page that contains a known javascript variable.
eg: var foo='my variable text';
How can I extract that value ('my variable text') using iMacros browser? Is that even possible to do directly?
Or, would I have to execute javascript to place that variable's value into an input or tag, that iMacros can then extract from?
Many thanks in advance.

You can use the URL GOTO method to call a javascript function, which will load the value inside the input element. Please checkout the below example where I have replicated the same.
HTML:
<script>
var a = "naren";
</script>
<input type="text" id="naren"/>
IMacros:
VERSION BUILD=9030808 RECORDER=FX
TAB T=1
URL GOTO=javascript:{(function<SP>(){console.log(document.getElementById(a).value='works!')})()}
So I have made a mockup page with the html as above, then ran the below imacros.
Some points you need to know about calling the imacros script are:
I have wrapped the anonymous function as an iife, long story short, the function will get executed immediately.
If you visit the IMACROS URL wiki page There is a line which tells us.
The URL GOTO command by default outputs the result of the evaluated
Javascript expression on a new page in the current tab. If you want to
manipulate an element on the page and don't want the result to be
processed by the URL GOTO command, you need to wrap it in some other
call or expression that does not evaluate to any value.
For example, to change the value of the Name field on the demo Fill
Form page, you can wrap the call in console.log() as follows:
URL GOTO=http://demo.imacros.net/Automate/TestForm1 URL
GOTO=javascript:console.log(document.getElementById("name").value='Test');
So I have implement the above statement by wrapping the assignment done to the input element inside a console.log.
Please let me know if you have issues implementing this solution!

Related

Java method doesnt execute from Javascript using Freemarker

I am trying to execute Java method in Javascript over object which I get from Java code using Freemarker.
I have java object "actual_id" of type "Settinga" and method "setActual_id" which just sets one attribute of the object. This is my call of the method in js:
var idd = ${actual_id}.setActual_id(variable);
Instead of execution in html code I see:
javaclassurl$Settinga#5beccfce.setActual_id(variable);
And there is error: myFunction is not defined
at HTMLButtonElement.onclick
Why it doesnt execute?
Following call is executed as expected but I need to include variable:
var idd = "${actual_id.setActual_id("aaa01dd3-abe4-4d50-a69e-62b04199b7c5")}";
I understand that Freemarker expressions are translated during page generation and when I use just string its easily interpreted and can be executed but during page generation my variable which I want to include in the call isnt known so need to make execution somehow else...
Because the way it works is this: FreeMarker is executed on the server, resolving all the ${...} to mere text, then the browser on the client side gets the resulting HTML and executes the JavaScript.

javascript - Global variable not working

I have a variable that I want saved so that multiple functions can use it. I followed w3schools's directions but it doesn't work. Am I forgetting something? Thank you in advance.
var name = document.getElementById('name').value;
function complete() {
document.getElementById('demo').innerHTML = name;
}
There are a few things to consider:
If you have code that attempts to find an element, but that element hasn't even been read by the browser yet, the browser won't be able to find it. So, make sure that your code only runs AFTER the full DOM has been loaded
by placing the script just before the closing body tag.
Don't attempt to get the value of a form field as soon as the page
loads because the user hasn't typed anything into it yet, so the
value will be nothing. You need to set up your code so that your function gets called at the right time (after the user has had a chance to type in the form field) so only get the value when that moment has come.
Don't give any element the name name because the Global window
object has a property called name that defaults to an empty string
and when you attempt to access name, it could incorrectly attempt
to get the window.name instead of your element called name.
Only form fields have a value property. All other elements have
.textContent (used when the string does not contain any HTML or you
want the actual HTML code displayed, rather than parsed) and
.innerHTML (used when the string does contain HTML and you want
that code parsed).
Lastly, do yourself a favor and don't use W3Schools. It is well known to have outdated or flat out wrong information on it. Instead use the Mozilla Developer's Network, which is recognized as one of the best resources for client-side web development documentation.
<input type="text" id="userName">
<input type="button" value="Click Me!" id="btn">
<div id="demo"></div>
<script>
// Set up the button's click event handling function
document.getElementById("btn").addEventListener("click", complete);
// Only get a reference to the element, not its value because, at this point,
// the user hasn't had a chance to type anything into it.
// Also, getting a reference to the element is the best approach because, if
// you decide you want some other property of the same element later, you'd have
// to scan the document for the same element again.
var theName = document.getElementById('userName');
function complete() {
// Now, just get the current value of the textbox at this moment in time
document.getElementById('demo').textContent = theName.value;
}
</script>

Javascript function call in url

I want to login a website with javascript and i dont know it is allowed. I use javascript code in url and it gives me Invalid left-hand side in assignment
at :1:10 error.
javascript:document.getElementById("OtherUsername")="myid";document.getElementById("OtherPassword")="mypassword";$("#btnSend").click();
As Vinod stated, you are trying to assign myid (a string) to document.getElementById("OtherUsername"), an object. That won't work. You need to assign it to document.getElementById("OtherUsername").value
This should work:
javascript:document.getElementById("OtherUsername").value="myid";document.getElementById("OtherPassword").value="mypassword";$("#btnSend").click();
The last bit $("#btnSend").click(); will only work if they have jQuery active on that site, or if you include it through use of a plugin somehow.
you can not Assign a value to a dom element
if OtherUsername element and OtherPassword element is a form you can follow my code
document.getElementById("OtherUsername").value="myid";
document.getElementById("OtherPassword").value="mypassword";
$("#btnSend").submit(); //btnSend should be the from id

can we create html tags using eval method in javascript

i found some code which is using anchor tags in eval
eval("a='http://google.co.in'+window .location.href");
The code does not yield an error at the same time it is not redirecting to the desired page. By observing this piece of code i want to know whether javascript eval() can be used to create html tags like eval('script tag') or not.
eval does only evaluate JavaScript code. To fetch a DOM tree from a string, one of the following methods can be used:
Set the innerHTML property of a DOM element.
document.write('..html here..') and document.writeln('..html here...').
Warning: The last method will overwrite the current document when the page has already finished loading.
Examples:
document.body.innerHTML = 'Stack';
Replaces the body with a single link.
document.write('Test');

how to call javascript function from client side on anchor click

I have javascript function sample('textValue') and have to call at server side on anchor click. I tried below code
string text="xyz";
anchor.Attributes.Add("onclick","javascript:sample('"+text+"');
but the value of the text is not assigning correctly. Encoded string gets added. The result in view source looks like
javascript:sample('xyz')
But i need javascript:sample('xyz')
What server/backend language do you use? PHP? Do you use any framework (Zend, CakePHP...)?
On the JS side do something like this:
Option 1
Test
Option 2
Test
<script type="text/javascript">
document.getElementById('clicky-clack-link').onClick = function() {
sample('test');
};
</script>
Note: Also check out jQuery if you haven't.
I wonder if you could just do this:
string text="xyz";
anchor.Attributes.Add("onclick", function(){ sample(text); } );
What does it do? Well, the onclick handler takes a function with no arguments, right? That is, what to do if somebody clicks the link. If you're coding this by hand in HTML, you can use the javascript:a_statement_goes_here to describe the code to run. I expect the browser will just create a function out of that. Since you're assigning this in JavaScript, you have to do that yourself (unless you write out to the document - that might work) and assign the function. But you don't have such a function yet - you have one sample that takes an argument - hence the anonymous function closing the text argument.
This is based on the assumption, that the above is actually client-side code. I'd be very surprised, if JS didn't allow you to assign a function to an attribute. In fact, I think the problem you are running into, is JavaScript trying to be very smart and make sure assigning a string, will stay a string - that is why your ' got encoded.
Have a go, tell me how it went. Ta!

Categories