PDA

View Full Version : iframe and submit button



shumbora
05-01-2006, 07:13 PM
hello evrey one, i was hopeing to build a search engine on my site that would search other sites like yahoo , google ,ect.. i've been having one problem with it tho , im trying to get the search results to open in an iframe but all my efforts do this have been in vain , i should mention that im a noob at this , that siad here is the code
this is my .js file (test.js)

function doSearch( selectedIndex, searchTerm ) {
if ( selectedIndex < 0 ) {
alert("Please select a search engine!");
return false;
} else if ( searchTerm == "" ) {
alert("No search term!");
return false;
}
window.open( SEARCH_DATA[selectedIndex][1] , SEARCH_DATA[selectedIndex][0] );
}



function engine1( selectedIndex, searchTerm )
{
SEARCH_DATA= new Array(73);
<!--SEARCH_DATA[value]= new Array( window name , url ) -->
SEARCH_DATA[1]= new Array("yahoo","http://search.yahoo.com/search?p="+escape(searchTerm)+"&sm=Yahoo%21+Search&fr=FP-tab-web-t&toggle=1&cop=&ei=UTF-8");


doSearch( selectedIndex, searchTerm );
return true;
}
this is the form code

<script language=JavaScript src="test.js" type=text/javascript></script>
<div align="center">
<form name="searchForm1" action="" onSubmit="javascript:engine1(this.engine.options[this.engine.selectedIndex].value,this.go.value); return false;">
<select name="engine" class="drop_down">
<option value="-1" selected>Torrent Search</option>
<option value="1">yahoo</option>

</select>
<input name="go" class="text_field" size="20" maxlength="35" />
<input name="submit" type="submit" class="search_button" value="Search" />
</form>
this is my iframe statment

<iframe id="inpage" style="width:600px;height:400px" src="http://www.google.com"></iframe>
im trying to set it up so that when the search button is clicked it would open the search results in the iframe. any comments will be appreciated and thanx in advance:D

jscheuer1
05-04-2006, 07:55 PM
Change:


window.open( SEARCH_DATA[selectedIndex][1] , SEARCH_DATA[selectedIndex][0] );

to:


inpage.location.href= SEARCH_DATA[selectedIndex][1] , SEARCH_DATA[selectedIndex][0] ;

There are probably many efficiency and error avoiding strategies that could be used as well but, the above change made it work.

Twey
05-04-2006, 08:10 PM
Ouch.

document.frames['inpage'].location.href= SEARCH_DATA[selectedIndex][1] , SEARCH_DATA[selectedIndex][0] ;

jscheuer1
05-04-2006, 10:17 PM
Ouch.

document.frames['inpage'].location.href= SEARCH_DATA[selectedIndex][1] , SEARCH_DATA[selectedIndex][0] ;


document.frames has no properties

Look before you leap, as I should have. The iframe's id is inpage, so for modern browsers we need:


document.getElementById('inpage').src= SEARCH_DATA[selectedIndex][1] , SEARCH_DATA[selectedIndex][0] ;

I tried various variations on changing id to name but it didn't like them in FF. I may have missed the correct one.

At least my code and your 'correction' both worked in IE6.

jscheuer1
05-04-2006, 10:28 PM
Oh, if we change:


<iframe id="inpage" style="width:600px;height:400px" src="http://www.google.com"></iframe>

to:


<iframe name="inpage" style="width:600px;height:400px" src="http://www.google.com"></iframe>

We can do this:


window.open( SEARCH_DATA[selectedIndex][1], 'inpage' ) ;

The ',SEARCH_DATA[selectedIndex][0]' was meaningless in our other versions, as far as I can tell, just a declaration of a variable. In the original, it was the name of the window, though I suppose it wasn't meant to be.

Twey
05-04-2006, 11:18 PM
Look before you leap, as I should have.Whoops... it's window.frames. My mistake. However, Firefox throws a series of exceptions when I try to set its source:
Error: uncaught exception: [Exception... "" nsresult: "0x804b000a (<unknown>)" location: "JS frame :: javascript:alert(window.frames['cw'].location="yahoo.com"); :: <TOP_LEVEL> :: line 1" data: no]
Error: uncaught exception: Permission denied to set property Window.src... which confused me slightly. I understand why this happens when trying to get the source, but why for a setter?

jscheuer1
05-05-2006, 02:17 AM
I'm not clear on the whys and wherefores but, going by what works, when you access an object like a frame as a window, you need to use its location property. When you access it as an element, use its source attribute. Kinda makes sense:


window.frames['inpage'].location.href = SEARCH_DATA[selectedIndex][1];

That's using the iframe with its name (not its id) set to 'inpage'. I'd forgotten about that window vs document thing. If the two of us have such trouble, you can imagine why it is so tough for the folks we help.

Twey
05-05-2006, 11:05 AM
when you access an object like a frame as a window, you need to use its location property. When you access it as an element, use its source attribute.That makes sense, but I still don't see the security risk in setting the location as we are (mistakenly) accustomed to doing with window.location.
If the two of us have such trouble, you can imagine why it is so tough for the folks we help.Hah, yes :)

jscheuer1
05-05-2006, 06:06 PM
That makes sense, but I still don't see the security risk in setting the location as we are (mistakenly) accustomed to doing with window.location.Hah, yes :)

I'm not sure I follow you. The location can be set when it is a window, the source can be set when it is an element. I believe these two can also be retrieved. Any security violation that occurs, say - accessing the source of a window or the location of an element, may be spurious, more a reflection of the fact that these objects technically do not possess the attribute or property in question and/or if they do, it represents something other than what you are imagining that it does.

Twey
05-05-2006, 07:11 PM
For example,
window.location = "www.google.com";will work fine. However,
window.frames[0].location = "www.google.com"; will throw a security exception. I don't see why.

jscheuer1
05-05-2006, 08:43 PM
window.frames[0].location.href = "www.google.com";

Twey
05-05-2006, 09:11 PM
I know that works. I was just wondering why
window.frames[0].location = "www.google.com";didn't, since String seems to be castable to whatever object window.location is.

jscheuer1
05-05-2006, 10:19 PM
Mike!

He'd know if anyone would. Probably the:

window.location="string"

Is a sort of illegal shorthand that all browsers allow. When it is applied to an object other than the window itself though, strict rules apply.

Twey
05-05-2006, 10:35 PM
Hm, perhaps, but that seems a lot less simplistic to me than simply allowing the location object to be cast to and from (or whatever the correct term is in a loosely-typed language) a string.

shumbora
05-14-2006, 11:39 PM
:) thanx guys for your comments , but i've got to work like so

<html>
<head>
<title>testing,,</title>
</head>
<body>
<script language=JavaScript src="test.js" type=text/javascript></script>
<div align="center">
<form name="searchForm1" action="" onSubmit="javascript:engine1(this.engine.options[this.engine.selectedIndex].value,this.go.value); return false;">
<select name="engine" class="drop_down">
<option value="-1" selected>Select a search engine</option>
<option value="1" >Google</option>
<option value="2">yahoo</option>
</select>
<input name="go" class="text_field" size="20" maxlength="35" />
<input name="submit" type="submit" class="search_button" value="Search" />
</form>
</div>
<iframe id="inpage" style="width:600px;height:400px" src="http://www.google.com"></iframe>

</body>
</html>

function doSearch( selectedIndex, searchTerm ) {
if ( selectedIndex < 0 ) {
alert("Please select a search engine!");
return false;
} else if ( searchTerm == "" ) {
alert("No search term!");
return false;
}
DoTheSearchInTheFrame(SEARCH_DATA[selectedIndex][1], SEARCH_DATA[selectedIndex][0]);
}

function engine1( selectedIndex, searchTerm )
{
SEARCH_DATA= new Array(73);
<!--SEARCH_DATA[value]= new Array( window name , url ) -->
SEARCH_DATA[1]= new Array("google","http://www.google.fr/search?hl=fr&q="+escape(searchTerm)+"&btnG=Recherche+Google&meta=");
SEARCH_DATA[2]= new Array("yahoo","http://search.yahoo.com/search?p="+escape(searchTerm)+"&sm=Yahoo%21+Search&fr=FP-tab-web-t&toggle=1&cop=&ei=UTF-8");

doSearch( selectedIndex, searchTerm );
return true;
}

function DoTheSearchInTheFrame(var_a,var_b)
{
eval("document.getElementById('inpage').src=var_a");
}

Twey
05-14-2006, 11:44 PM
Sorry... what's the point of that eval() call?

shumbora
07-29-2006, 12:48 AM
Sorry... what's the point of that eval() call?
i honestly dont know :D it couldnt be calling my house for all i know , see im a complete noob.. i've had help from a guy called GBS over @ the phpfreaks forum. http://www.phpfreaks.com/forums/index.php?showtopic=92509

mburt
07-29-2006, 12:57 AM
Huh? All that's been said is VERY confusing. I prefer to do it like this: (i'm prepared to be corrected :))

<html>
<head>
</head>
<body>
<form>
<input type="button" value="Mamma.com" onclick="inline.window.location.href='http://www.mamma.com'">
<br><iframe id="inline"></iframe>
</form>
</body>
</html>