0

Currently I am working on a bit of a easy setup, but I am confused how to proceed any further. I have exhausted roughly 4 hours looking for the right solution. What I basically am trying to do is pass values from the URL to an iFrame on my Sharepoint page. I setup a basic webpart page, added a content editor webpart. I then added my code in the text file and then attached my text file to the content editor webpart after uploading it to the Site Assests folder. I am still unable to get the result i need.

I want the following to happen.

User types in the following URL: www.sharepoint.com/home.aspx?123xyz

The page loads and the iFrame source is updated to: www.abc.com/123xyz

I know in order to do this I need to use JavaScript I have attempted to use two of them listed below. Any help is greatly appreciated.

This is the first method:

<script language="javascript" type="text/javascript">

  $(function() {
    var loc = window.location.toString(),
    params = loc.split('?')[1],
    iframe = document.getElementById('myIframe');
    iframe.src = 'wwww.abc.com/?' + params;
  });
</script>

<iframe name="myIframe" src="about:blank" style="position:absolute; height:100%; width:100%; border:none;"></iframe>`

This is the second Method:

<script type="text/javascript">
  alert('test error message');
  $(function() {
    var loc = window.location.toString(),
    params = loc.split('?')[1],
    iframe = document.getElementById('myIframe');
    iframe.src = iframe.src + '?' + params;
  });
</script>

<iframe id="myIframe" src="www.abc.com/" style="position: absolute; height: 100%; width: 100%; border: none;"></iframe>`
2

When you use getElementbyId, make sure the element which you're selecting, contains that ID. From your 1st method, it is wrong because your iframe used the name property, the 2nd method is correct.

The reason your code isn't work is most likely because jQuery isn't loaded. Here's a revision of your code which works (note, I am not using jQuery, just standard Javascript):

<script type="text/javascript">
    var loc = "http://jsfiddle.net?123xyz";
    var params = loc.split('?')[1];
    document.getElementById("myIframe").src = 'wwww.yahoo.com/?' + params;
</script>

<iframe id="myIframe" src="http://www.w3schools.com" width="80%"></iframe>

JS Fiddle http://jsfiddle.net/gbgc603h/

Please note, I hard-coded the location in order to achieve the same URL in which you're using. Ignore the blah as it was just testing the current URL.

  • This is perfect thank you for the guidance, i was able to achieve what I had wanted. Thank You – Ahsan Iftikhar Dec 11 '15 at 14:41
  • You should also add data validation, to validate if there are any params, if there are none, then, you should not redirect. – balowknee Dec 11 '15 at 17:29
0

I see you are using Jquery have you included the Jquery library? I am not sure what exactly you are trying to achieve but depending on the SharePoint version can't you use Query String Filter webpart. In order to debug you can check the developer tool (f12) console to see if there are any errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.