CSharp - 通过javascript设置iframe的useragent

我想在iframe中加载现有页面,模拟iphone的User Agent,


<script type="text/javascript">


 navigator.__defineGetter__('userAgent', function () {
 return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
 });

 var win = window.open('/home/get');
 win.navigator.__defineGetter__('userAgent', function () {
 return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
 });
 win.location.href = '/home/get'; //required


 $(function () {
 var frame = $('<iframe width="320" height="480"></iframe>');
 frame.hide();
 $('#container').append(frame);


 (frame[0].contentWindow || frame[0].contentDocument).navigator.__defineGetter__('userAgent', function () {
 return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
 });
 frame.attr('src', '/home/get');

 });


 frame.fadeIn();
 });
 </script>

时间:


 $(function () {
 var frame = $('<iframe width="320" height="480"></iframe>');
 frame.hide();

 $('#container').append(frame);
 var contentWindow = frame[0].contentWindow || frame[0].contentDocument;

 var setUA = function() {
 if (Object.defineProperty) {
 Object.defineProperty(contentWindow.navigator, 'userAgent', {
 configurable: true,
 get: function () {
 return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
 }
 });
 } else if (Object.prototype.__defineGetter__) {
 contentWindow.navigator.__defineGetter__('userAgent', function () {
 return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
 });
 } else {
 alert('browser not supported');
 }
 };

 $.ajax({
 cache: false,
 url: '/home/get',
 success: function (html) {
 contentWindow.document.open();
 setUA();
 contentWindow.document.write(html);
 contentWindow.document.close();
 frame.fadeIn();
 }
 });
 });

...