I am passing div name in the query string from one html page and retrieving that div name on the other html page. Now I want to display that specific div on the page.My code is
function onLoad()
{
var divname=window.location.search.substring(1);
document.getElementById(divname).style.display="block"; //error is in this line
}
But I am getting an error as "object expected". please help me
The window.location.search property returns the part of the URL that follows the ? symbol, including the ? symbol.
So for example it might return ?paramname=paramvalue. When you call substring(1) on it you get paramname=paramvalue which is what gets passed to the document.getElementById function which obviously is wrong because such element does doesn't exist on your DOM.
You could use the following javascript function to read query string parameter values:
function onLoad() {
var divname = getParameterByName('divname');
document.getElementById(divname).style.display = 'block';
}
This assumes that you have a query string parameter name called divname:
?divname=some_div_name
Adjust the parameter passed to the getParameterByName function if your query string parameter is called differently.
You might also want to introduce error checking into your code to make it more robust:
function onLoad() {
var divname = getParameterByName('divname');
var divElement = document.getElementById(divname);
if (divElement != null) {
divElement.style.display = 'block';
} else {
alert('Unable to find an element with name = ' + divname);
}
}
What I am suggesting is place your js at the end of the html code (before </body> tag). Do not use a function.
<html>
...
...
...
<body>
...
...
...
<script>
var divname=window.location.search.substring(1);
document.getElementById(divname).style.display="block";
</script>
</body>
</html>
I have re-written my function and it is working, code is like this
function load()
{
var divname = window.location.search.substring(1);
var params=divname.split('=');
var i=1;
alert(params[i].substring(0));
document.getElementById(params[i].substring(0)).style.display='block';
}
Related
So I have a main html-document called commit_PNG.html and in this document i have two simple string variables i want to use in the other html-document called popup.html. At the moment i have a function like this:
<script type="text/javascript">
function PopUpFenster(id) {
myWindow = window.open('popup.html?id='+id, 'Info window', 'height=350, width=800');
}
</script>
In the second html-document I want to work with the string variables. I need a solution that work something like this in popup.html:
var string1 = "http://www.test.com/"+ commit_PNG.stringvariable1;
var string2 = "http://www.test.com/"+ commit_PNG.stringvariable2;
I'm not sure but I either need to take them directly from commit_PNG.html or parse them someone with the window.open() method.
Use the hash part to transfer a JSON object like this:
In commit_PNG.html:
var myStrings = {
str1:"my first str",
str2:"my second str"
}
function PopUpFenster(id) {
var myUrl = "popup.html?id="+id+"#"+encodeURIComponent(JSON.stringify(myStrings));
window.open(myUrl , "Info window", "height=350, width=800");
}
Then in your popup.html Just do:
var myData = JSON.parse(decodeURIComponent(window.location.hash.substring(1)));
alert(myData.str1 + " " + myData.str2);
It's a great way to pass date in a url. You get to pass a JSON object and using stringify with encodeURIComponent makes it a safe string for the URL.
Using the hash part, makes sure it's not sent to the server.
commit_PNG.html
<script type="text/javascript">
function getVariables(){
return {
stringvariable1: 'v1',
stringvariable2: 'v2'
};
}
function PopUpFenster(id) {
myWindow = window.open('popup.html?id='+id, 'Info window', 'height=350, width=800');
}
</script>
popup.html
<script type="text/javascript">
var parentWindow = window.opener;
var variables = parentWindow.getVariables();
var string1 = "http://www.test.com/"+ variables.stringvariable1;
var string2 = "http://www.test.com/"+ variables.stringvariable2;
</script>
i have a String in html which should is post loaded in body.
var str = new String("<span>Hello</span><script>window.alert('hello');</script>");
document.getElementsByTagName('body')[0].innerHTML=str;
but the alert does not come. so i added this:
for(i=0;scr=document.getElementsByTagName('body')[0].getElementsByTagName('script')[i];i++) {
eval(src.innerHTML);
};
but even it doesn't work. can u solve my problem?
document.getElementsByTagName('body')[0].innerHTML="<span>Hello</span><script>window.alert('hello');<\/script>";
var alert_func = document.getElementsByTagName('script')[0].innerHTML
if(alert_func.indexOf("alert") != -1) {
eval(alert_func);
}
This code will first set the innerHTML of the <body> tags to <span>Hello</span><script>window.alert('hello');</script>. Then a variable alert_func will store the content between the <script> tags. If there is the word "alert" in alert_func, we will evaluate whatever is between the <script> tags and give the alert.
How do you completely replace a function in JavaScript?
I got this code, but it doesn't work. The DOM gets updated, though. What's up with that?
<html>
<head>
<script id="myScript" type="text/javascript">
function someFunction() {
alert("Same old.");
}
</script>
</head>
<body>
<input type="button" onclick="someFunction();" value="A button." />
<script>
function replace() {
var oldFunctionString = someFunction.toString();
var oldContents = oldFunctionString.substring(oldFunctionString.indexOf("{") + 1, oldFunctionString.lastIndexOf("}") );
var newCode = "alert(New code!);";
var newFunctionString = "function someFunction(){"+newCode+"}";
var scriptTag = document.getElementById('myScript');
scriptTag.innerHTML = scriptTag.innerHTML.replace(oldFunctionString,newFunctionString);
}
replace();
</script>
</body>
</html>
JSfiddle here
Setting .innerHTML doesn't re-execute a script. If you really wanted to do that, you'd have to create a new script element and append it to the DOM, which then overwrites what the previous script has done (not possible in all cases, of course).
If you want to replace that function, just use
somefunction = function() {
alert(New code!); // syntax error, btw
};
Of course, to replace only parts of the code (not knowing all of it) you could try regex and co. Still just reassign the new function to the variable:
somefunction = eval("("
+ somefunction.toString().replace(/(alert\().*?(\);)/, "$1New code!$2")
+ ")");
It seems you are trying to work with strings, not the function itself. Just do this instead:
someFunction = function () { /* your function code here */ }
I'm completely new at javascript and I'm wondering about something really elementary here. I've got an iFrame that I want a dynamic src on. This src(source) should just be a variable. And then a script sets that variable before the frame is loaded.
It's actually a webpart in Sharepoint 2010, so I set up the webpart and edit it's HTML source to something like this:
<script language="JavaScript">
var qs = getQueryStrings();
var myParam = qs["myParam"];
function getQueryStrings() {
var assoc = {};
var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
var queryString = location.search.substring(1);
var keyValues = queryString.split('&');
for(var i in keyValues) {
var key = keyValues[i].split('=');
if (key.length > 1) {
assoc[decode(key[0])] = decode(key[1]);
}
}
return assoc;
} </script>
<iframe height="500" src="(myParam);" width="800"></iframe>
I'm not even sure the syntax is correct. Basically, I want to insert the variable into the src of the iframe.
you have to give some class or ID to your Iframe.
And then you can call a function which will give src to i frame dyanmically.
from client side use this:
$('#ID_of_Iframe').attr('src','NEW SRC hERE');
Example: $('#ID_of_Iframe').attr('src','www.google.com');
Make your links like this:
File1.PDF
or:
<iframe name='myPdfFrameName'></iframe>
File1.PDF
function loadFrame(href) {
window.frames['myPdfFrameName'].location = href;
return false;
}
EDIT: Easiest is probably using target attribute of a link:
<a href='file1.pdf' target='myPdfFrameName'>File1.pdf</a>
I am using the following javascript to retrieve a value of a asp hidenfield.
var pagemode
function setValue() {
pagemode= document.getElementById('<%#litTest.ClientID%>').value;
}
function Item_load(sender, eventArgs) {
window.location.href = "Request.aspx?Request_ID=" + eventArgs.get_item().getMember('Request_ID').get_value() + "&ListType=" + pagemode;
}
The value to the hiddenfield is loaded in page load in code behind. I need to pass the value as a querystring and it comes as undefined.
I would really appreciate if anyone can help.
So you don't get a reference to your hiddenfield? Try this:
<script type="text/javascript">
var litTestID = '<%= litTest.ClientID %>';
function setValue() {
var pagemode = $(litTestID).value;
// ...
}
</script>
Otherwise use the javascript debugger to inspect the order of executino and the variable values.