Best web-development questions in March 2012

Jump Link Inside an iFrame

12 votes

Inside an iframe (on page-A), I have a simple page (page-B) that has a few jump links (e.g. <a href="#my-id">jump link</a>) to different sections of the page (page-B). The iframe height is preset to be longer than page-B's height; this is a requirement.

For some reasons, the jump links didn't work on FF (I am on Mac/FF 10.0.2); however, it worked properly on Safari and IE8. This is the sample page.

Code of page

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jump Link Test on an iFrame</title>
</head>
<body>
<h1>Page that has an iFrame</h1>
<iframe width="100%" height="2000" src="./iframe.html" frameborder="0" scrolling="no">
</iframe>
</body>
</html>

Code of iframe.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>iFrame Content</title>
<style type="text/css">
.box {
    margin: 0 0 5px;
    width: 400px;
    height: 400px;
}
#box1 {
    background-color: #f00;
}
#box2 {
    background-color: #f0f;
}
#box3 {
    background-color: #00f;
}
</style>
</head>
<body>
<ul>
    <li><a href="#box1">Box 1</a></li>
    <li><a href="#box2">Box 2</a></li>

    <li><a href="#box3">Box 3</a></li>
</ul>
<div>
    <div id="box1" class="box"></div>
    <div id="box2" class="box"></div>
    <div id="box3" class="box"></div>
</div>
</body>
</html>

Note: If I set the iframe height < page-B's height, the problem will be solved. However, unfortunately this isn't an option given my situation because I have no access to page-A.

This is not possible with HTML only.

As you can read on the Firefox Bug Report Nr. 638598 this is mentioned long time ago! Also many people don't like that behavior, but Jonas Sicking says in his comment that this will never change. He sees that as a feature that firefox prevent this potential hacking functionality.

if you don't know him read here that he is the Tech Lead of the project at mozilla as well as an editor for the and specifications at W3C.

Other people tried to find solutions like Matthew but this example didn't work in my short test case with your html structure. Some others say that it should work with JavaScript and the scrollTo() Function.

I'm sorry for saying that this is a limitation of FireFox only, but hope you are happy to be safe in the knowledge about that problem.

Get running session count from IIS for my hosted Asp.Net web site

7 votes

I am hosting an Asp.Net website in IIS 6.0

We have to reset the session timeout in web.config

My client want me to reset it only if no session is running (no one is using the site).

We have not used Membership and SessionState is set to InProc

Can I know if anybody using the site or any session is running for my application in IIS.

I can't make change in source code or any other file except web.config in the hosted website.

I'm not great at PowerShell, so hopefully you can look up the proper syntax. But ...

One option is to run a Powershell script and check the count of the session like this:

UPDATE: Changed 'Sessions Total' to 'Sessions Active'

write-host Getting performance counters ...

$perfCounterString = "\asp.net applications(__total__)\sessions active" 
$counter = get-counter -counter $perfCounterString 
$rawValue = $counter.CounterSamples[0].CookedValue 

write-host Session Count is $rawValue

if( $rawValue -gt 0)
{
   write-host Session are active - will not stop IIS
   exit
}

write-host Stopping IIS
stop-service "IISAdmin"

# Set values 
$webConfig = "d:\web.config"
$newTimeout = "20"

# Open file and change value
$doc = new-object System.Xml.XmlDocument
$doc.Load($webConfig)
$doc.SelectSingleNode("//sessionState").timeout = $newTimeout
$doc.Save($webConfig)

write-host Starting IIS
start-service "IISAdmin"
write-host Done!

Save this on the desktop as "ChangeIIS.ps1".

Now Powershell doesn't like you just running scripts like .bat files. You have to grant access to the process or your user for security reasons. So do the following:

  1. Open a command prompt and Run As Administrator
  2. Type powershell
  3. Type Set-ExecutionPolicy -scope process Bypass
  4. Type sl <path to directory of .sp1 file> . sl [set-location] is like cd in command prompt
  5. Type $ '.\ChangeIIS.ps1'

It will run now and reset the value.

Here is a link to my blog on how I created the PowerShell script in a more Step-by-step fashion

What is the best method to make sure two people don't edit the same row on my web app?

6 votes

I have a PHP/jQuery/AJAX/MySQL app built for managing databases. I want to implement the ability to prevent multiple users from editing the same database row at the same time.

  1. What is this called?
  2. Do I use a token system and who ever has the token can edit it until they release the token?
  3. Do I use a "last edit date/time" to compare you loading the HTML form with the time in the database and if the database is the most resent edit then it warns you?
  4. Do I lock the row using database functions?

I'm just not sure which is the best. Assuming between 10 - 15 concurrent users

There are two general approaches-- optimistic and pessimistic locking.

Optimistic locking is generally much easier to implement in a web-based environment because it is fundamentally stateless. It scales much better as well. The downside is that it assumes that your users generally won't be trying to edit the same set of rows at the same time. For most applications, that's a very reasonable assumption but you'd have to verify that your application isn't one of the outliers where users would regularly be stepping on each other's toes. In optimistic locking, you would have some sort of last_modified_timestamp column that you would SELECT when a user fetched the data and then use in the WHERE clause when you go to update the date, i.e.

UPDATE table_name
   SET col1 = <<new value>>,
       col2 = <<new values>>,
       last_modified_timestamp = <<new timestamp>>
 WHERE primary_key = <<key column>>
   AND last_modified_timestamp = <<last modified timestamp you originally queried>>

If that updates 1 row, you know you were successful. Otherwise, if it updates 0 rows, you know that someone else has modified the data in the interim and you can take some action (generally showing the user the new data and asking them if they want to overwrite but you can adopt other conflict resolution approaches).

Pessimistic locking is more challenging to implement particularly in a web-based application particularly when users can close their browser without logging out or where users may start editing some data and go to lunch before hitting Submit. It makes it harder to scale and generally makes the application more difficult to administer. It's really only worth considering if users will regularly try to update the same rows or if updating a row takes a large amount of time for a user so it's worth letting them know up front that someone else has locked the row.