• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with JavaScript Array

DrMoreau

Banned
My JavaScript skills only go so far and as I plunge in a bit deeper I have hit a wall on this one.

I got this array to work but I just know it can be written better:

var data = new Array();
data[1] = "<iframe src='news.htm' width=100% height=100% frameborder=0 scrolling=no></iframe>";
data[2] = "<iframe src='alaska.htm' width=100% height=100% frameborder=0 scrolling=no></iframe>";

Notice that the only difference between the two sets is the source while everything else remains identical.

Is there a way to write this where the <iframe> properties are already defined, like in another variable, and the only thing that needs to be specified in the array would be the source?

For Example:

var data = new Array();
data[1] = "src='news.htm'";
data[2] = "src='alaska.htm'";

Thus making it easier to update when the source needs to be changed.
Thanks.
 
You could do:

Alternative 1
----------------

<script>
var part1 = "<iframe src=...."; // all the first part stuff
var part2 = ">...</ifram>"; // all the second part stuff
document.write(part1 + ( condition? data[1] : data[2] ) + part2);
</script>

Or:

Alternative 2 - The Better Way
--------------------------------------

Somewhere in your head tag define:

<script>
var data = new Array();
data[1] = "file1.html";
data[2] = "file2.html";

function getSource() {
if( condition )
return data[1];
else
return data[2];
}

</script>

And in your HTML source:

<iframe src="javascript:getSource()"></iframe>
 
Back
Top