473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to change src value in javascript?

I want to change the advertisement url for different browser/device viewport width. So far, I found a code to detect viewport width, I need detailed help to change the url. Thanks in advance!
This is the code for the viewport width:
Expand|Select|Wrap|Line Numbers
  1. var viewportwidth;
  2.  if (typeof window.innerWidth != 'undefined')
  3.  {
  4.       viewportwidth = window.innerWidth
  5.  }
  6.  else if (typeof document.documentElement != 'undefined'
  7.      && typeof document.documentElement.clientWidth !=
  8.      'undefined' && document.documentElement.clientWidth != 0)
  9.  {
  10.        viewportwidth = document.documentElement.clientWidth
  11.  }
  12.  else
  13.  {
  14.        viewportwidth = document.getElementsByTagName('body')[0].clientWidth
  15.  }
Now, this is what I want to do:
Expand|Select|Wrap|Line Numbers
  1. if ( viewportwidth > 1024 )
  2.  {
  3. use this ads: <SCRIPT  SRC="url for big banner" type="text/javascript"></SCRIPT>
  4.  }
  5. else {
  6. use the ads:<SCRIPT SRC="url for small banner" type="text/javascript"></SCRIPT>
  7. }
Jan 5 '11 #1
7 1606
Samishii23
246 100+
http://www.hunlock.com/blogs/Howto_D...script_And_CSS

Taken from above link near bottom of page:
Expand|Select|Wrap|Line Numbers
  1. function startup() {
  2.    var headID = document.getElementsByTagName("head")[0];         
  3.    var newScript = document.createElement('script');
  4.    newScript.type = 'text/javascript';
  5.    newScript.src = 'http://api.flickr.com/services/feeds/photos_public.gne?tags=sunset&format=json';
  6.    headID.appendChild(newScript);  
  7. }
  8. <body onload='startup()'>
Jan 6 '11 #2
Thanks! But this code doesn't say the condition of viewport. I need to inset a big banner if viewport>1024, and insert a small banner if viewport<1024
How to add this condition in your code?
Jan 6 '11 #3
Samishii23
246 100+
Are you trying to get the users resolution width and height?
Jan 10 '11 #4
yes, I got the user's browser viewport width via first javascrip. Now I need to deliver different content for different width.
Jan 10 '11 #5
Samishii23
246 100+
You talking about something like resizing the HTML content based on what you get back from the View Port?
If thats all you want to do you could just simply in your HTML style declarations, just use %'s when dealing with widths, maybe not as much with heights, but with %, the browser will dynamically size the HTML content for you based on how big the window is.
Expand|Select|Wrap|Line Numbers
  1. <style>
  2. #ContentNav {
  3.   width: 15%;
  4.   min-width: 100px; /* Won't resize below 100px */
  5.   }
  6. #ContentBody {
  7.   width: 80%;
  8.   min-width: 300px;
  9.   }
  10. </style>
Jan 10 '11 #6
Samishii23
246 100+
After looking through Mozilla Developer Network min-width compatibility reference, if you want to not use %'s that be fine. However, if it were me, I would typically create a webpage to normally be around 600-800px wide max anyways. Most based on content and what not.

Most people today don't normally have computers with resolutions below 1024x768 anyways. If they do then chances are they are not browsing the web with it. I would just keep my website's width below 1024, and for the most part you should be fine.

Just my 2 cents though.

If you still want to resize the pages content I would do something like this:
Expand|Select|Wrap|Line Numbers
  1. var standardW = [800,1024,1152,1280,1400]; // these are just the resolutions my monitor supports
  2. var desiredW = []; // fill with what you want your widths to be based on monitor View Port, and make sure you match what you want with the same array #
  3. // Again heights are not really a big deal unless you want your webpage to exactly fit the screen.
  4.  
  5. for ( var i=0; i<standardW.length; i++ ) {
  6.   if ( viewportwidth == standardW[i] ) {
  7.     document.getElementById("ContentBody").style.width = desiredW[i];
  8.     break;
  9.     }
  10.   }
There will always be a chance that a user will have a random obscure width size, so if the standardW isn't reached, you'll have to check the viewport to be within a certain range. like:
Expand|Select|Wrap|Line Numbers
  1. if ( viewportwidth > 800 && viewportwidth < 1024 ) { ... }
This is another reason why I'd just make a webpage just one size. Just not too huge.
Jan 10 '11 #7
The banner ads can't be done this way. Beside the banner is javascript in itself, the size and contents are done when we choose ads.
So, need to change the whole banner script block.
Jan 10 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out...
3
by: John Smith | last post by:
I'm looking into this peace of code: protected void DropDown_SelectedIndexChanged(object sender, EventArgs e) { DropDownList list = (DropDownList)sender; TableCell cell = list.Parent as...
2
by: John Smith | last post by:
Will this line of the code: item.Cells.Text = "Some text..."; change only DataGrid visual value or it will also change value in the DataSource? How can I change value in DataSource? ...
0
by: marknoten | last post by:
Hi, we are working with XML files that can be as big as 6Mb. Some XML documents that obey to a certain condition must be duplicated where only 2 elements need to change value. I thought using...
2
cassbiz
by: cassbiz | last post by:
I may be in the wrong forum so Ronald don't shoot :) In my code I have an option box to choose a number - works fine. I want to carry over the new value to another field to do a recalculation. ...
1
by: PawelR | last post by:
Hi Group, In my application I have DataTable which is displayed in DataGridView via DataView: DataView myView = new DataView(myTable); myDataGridView.DataSource = myView; One column im...
3
by: rag84dec | last post by:
Hi, i want to change the hidden elemtn value in the javascript. the name of the filed will have to be formed in the javascript. like this var temp="name"+1; document.temp.value="newname";
1
by: Jafri256best | last post by:
I want to change value of a variable permanently during executing a form and want to enter the value through the text box.
4
by: mikael3432 | last post by:
hey. I wonder if anyone can help me with this one: I want to change value 21 to 210 in colum A, given there is another row that has value 10 in colum A and in both rows values in colum B, C D are...
0
by: wasim jack | last post by:
sir,I want to change value of combobox of datagridview on the basis of previous combobox value of the same raw of same datagridview
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.